Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PrintWriter strange behaviour

699554Dec 5 2009 — edited Dec 6 2009
PrintWriter outputStream = new PrintWriter(new FileWriter(filenameOut));
BufferedReader inputStream = new BufferedReader(new FileReader(filenameIn));
I create a PrinterWriter like so, then I read a line of text from a text file using a BufferedReader wrapped around a FileReader. Each line of text is read by BufferedReader's readLine() method. And each line is written using the PrintWriter's println() method.

The weird behaviour occured while running my application twice, the first iteration and second iteration output different Strings to a text file, using the same println() method of PrintWriter and each String not containing a newline character. The lengths of the two files differed, one was off by 10 lines after reading in 50 thousand lines. It took me twenty minutes to plug in flush() after every invocationg of the println() method from PrintWriter to resolve the issue. Both the PrintWriter and BufferedReader were closed after processing.

Any possible explanations for this? I assume it's not a bug but my own personal misuse.

Mel
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2010
Added on Dec 5 2009
6 comments
747 views