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

Thread: Java error while using BufferedReader class to read a .txt document

  1. #1
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Question Java error while using BufferedReader class to read a .txt document

    I'm trying to write my first I/O program and I seem to have a problem with using the BufferedReader class. Every time I read in from a .txt document and then print out to a different one it prints out every other line, starting with the second line. I'm pretty sure it is not the PrintWriter's fault because I did not have a problem with printing strings I defined in the program. When I put a line space in between each line the output was the way I wanted, but the file is a dictionary file with hundreds of thousands of words so I can't possibly manually enter all those spaces. Could someone please help me with this problem? I was told something about using flush, and I set the automatic flush argument to true for the PrintWriter, which didn't seem to have any effect.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: BufferedReader skipping lines?

    Hey Jchang504,

    Could you post your code here please for me to take a look at? I think I know what your problem is but i'll need to see your code to confirm.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: BufferedReader skipping lines?

    The input file was a file I used to test if it would work that contained the following:
    AA
    BB
    CC
    DD
    What was printed in the output file was:
    BB
    DD
    Here is the code:

    /*Temporary program for reprinting the dictionary file.
    Created on January 3, 2009*/
     
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
     
    public class SortDic
    {
    public static void main(String[] args) throws IOException
    {
    String placeHolder;
    FileReader fr = new FileReader("TestFile.txt");
    BufferedReader readWord = new BufferedReader(fr);
    FileWriter fw = new FileWriter("TWL_Dic.txt");
    PrintWriter printWord = new PrintWriter(fw, true);
     
    System.out.println("Working...");
    while(readWord.readLine() != null)
       {
       placeHolder = readWord.readLine();
       printWord.println(placeHolder);
       }
    System.out.println("Done.");
     
    readWord.close();
    printWord.close();
     
    }
    }

    Thanks for your help.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: BufferedReader skipping lines?

    You're reading two lines each time round the loop, and not writing the first one. Your loop condition does the first readLine, then you do another readLine inside the loop and write that line. The other problem with the loop is that you should test the value returned from readLine before trying to write it. As it is, you'll read the null at end of file and try to process it. It may not crash a print statement, but it's not good.

    The classic idiom for this is to read once before the loop, then again at the bottom of the loop: (pseudo-code)
    value = readLine()
    while value != null
       process(value)
       value = readLine()
    end while
    Some coders often use a compact version that crams the value read-ahead and the loop condition into a single loop expression: (pseudo-code)
    while (value=readLine()) != null
       process(value)
    end while
    IIRC this idiom first became popular in C & C++, but tends to be considered less than Best Practice by many these days, as it involves an unnecessary compound expression and uses side effects, but it's still fairly popular with those who prefer syntactical economy over simplcity and clarity.
    Last edited by dlorde; February 2nd, 2009 at 06:35 PM.

  5. #5
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: BufferedReader skipping lines?

    I can't believe I didn't see this. It seems so obvious now. I thought that the loop condition just verified that the first line was not null and then the read the first line again for the printing, but it makes sense that the loop condition is actually a part of the loop that executes every time. Thanks for all the help, the answer was simpler than I thought!

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: BufferedReader skipping lines?

    Nice one dlorde. Good answer

    Jchang504, I have marked this thread as Solved. Please take a look at this link so you can do it yourself in the future:

    http://www.javaprogrammingforums.com...-new-post.html

    Thanks guys
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member Jchang504's Avatar
    Join Date
    Nov 2008
    Posts
    28
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: [SOLVED] BufferedReader skipping lines?

    Thanks for your help, JavaPF. I will mark my threads when they are solved from now on.

Similar Threads

  1. How to assign values from a file to an arraylist of objects?
    By nadman123 in forum Collections and Generics
    Replies: 3
    Last Post: September 15th, 2008, 01:07 PM
  2. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM

Tags for this Thread