kkudi
kkudi

Reputation: 1645

java - PrintWriter with FileWriter and BufferedWriter

I'm at my wits end here.

I'm trying to print a few thousands of lines in a file, using the following:

        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true));

        PrintWriter pw = new PrintWriter(bw, true);

The file already consists of text so I'm appending, hence the true argument, in FileWriter.

Now what seems to be puzzling me for the last two hours, is that around 85-90% of the lines get written into the file, while the FIRST 10-15% are not.

There's nothing wrong with the code in terms of logic, because if i print it in the console, all lines are printed.

Am I missing something here?

I only do pw.close() after all output is printed.

Upvotes: 1

Views: 2368

Answers (2)

Vern
Vern

Reputation: 2413

You might want to invoke a manual .flush() command after each time you write to your file in your code just to be very sure that you are writing out correctly.

This is pretty puzzling, do write back if the problem persists.

Hope it helps!

Cheers, Vern

Upvotes: 2

Jasonw
Jasonw

Reputation: 5064

before pw.close(), perhaps you should call flush() to ensure all the stream is written out.

Upvotes: 0

Related Questions