Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Reading comma seperated text file (http://www.go4expert.com/forums/reading-comma-seperated-text-file-t28803/)

rasty2g2 29Jul2012 15:42

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
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

smohd 21Aug2012 16:46

Re: Reading comma seperated text file
 
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 15Sep2012 16:52

Re: Reading comma seperated text file
 
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


All times are GMT +5.5. The time now is 10:02.