Reading comma seperated text file

rasty2g2's Avatar, Join Date: Jul 2012
Newbie Member
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
Code:
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

Last edited by shabbir; 29Jul2012 at 18:42.. Reason: Code blocks
smohd's Avatar, Join Date: Aug 2012
Light Poster
I dont think i f I understand correctly what you are doing. But if you need to split the line string which you have read, then just use split function from String class(read the doc, I cant post the link yet).
Example
Code:
strLine.split(",")
will return an array of strings by splitting the strLine with ,
ManzZup's Avatar, Join Date: May 2009
Skilled contributor
yes didnt understand the problem much but i guess split would work
if you know the lengths exactly you can use substring as well
but i do reccomend using the DOM/SAX for interacting with XML rather than writting by yourself