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.
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.
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:
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.
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)
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)
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.
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!
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 :D
Re: [SOLVED] BufferedReader skipping lines?
Thanks for your help, JavaPF. I will mark my threads when they are solved from now on.