Reading comma seperated text file
Hi,
I need to read the contents of a text file which contains comma separated values into my code. Code iterates until last line in text file is read, then ends. Now i am able to read a line with "strLine". See my code below so far:
TEXT FILE CONTENT (test.txt)
00000000, 05007
00000001, 05487
00000002, 05456
import java.io.*;
public class ODBmain
{
public static void ODBmain()
{
// initialise instance variables
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("test.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
FileWriter fileWriter = new FileWriter("test.xml");
BufferedWriter buffWriter = new BufferedWriter(fileWriter);
while ((strLine = br.readLine()) != null) {
buffWriter.write("<sn:SuMSubscriberProfile id=\"233" + strLine + "\">");
buffWriter.newLine();
buffWriter.write("<kk>05007</kk>");
buffWriter.newLine();
How do i separate strLine so i can read and iterate second field 05007, 05487 in kk.
Thanks
Re: Reading comma seperated text file
Quote:
How do i separate strLine
The String class's split() method will separate tokens on a line into separate elements in an array.
Re: Reading comma seperated text file
How do i refer to returned values for String.split() in my code for COLUMN1 and COLUMN2 please?
while ((strLine = br.readLine()) != null) {
strLine.split(",")
buffWriter.write("<sn:SuMSubscriberProfile id=\"233" + COLUMN1 + "\">");
buffWriter.newLine();
buffWriter.write("<kk>COLUMN2</kk>");
buffWriter.newLine();
...
Re: Reading comma seperated text file
Well, the split function returns an array of strings. So you need a String array, and assign the split function to that array.
Re: Reading comma seperated text file
The split() method returns a value (an array) that has the separated tokens.
array = string.split(...);