Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Reading comma seperated text file

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Reading comma seperated text file

    How do i separate strLine
    The String class's split() method will separate tokens on a line into separate elements in an array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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();
    ...

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,421
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Reading comma seperated text file

    The split() method returns a value (an array) that has the separated tokens.
    array = string.split(...);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Not Reading From Text File
    By JavaLaxer15 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2012, 11:16 AM
  2. simple program for reading text file
    By johnpipes in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2011, 04:55 PM
  3. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 04:31 PM
  4. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  5. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM