1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Reading comma seperated text file

Discussion in 'Java' started by rasty2g2, Jul 29, 2012.

  1. rasty2g2

    rasty2g2 New Member

    Joined:
    Jul 29, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Jul 29, 2012
  2. smohd

    smohd New Member

    Joined:
    Aug 21, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Zanzibar
    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 ,
     
  3. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    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
     

Share This Page