1

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.

6
  • Can you post some more of the code? Can you try a pw.flush() -- not sure if it will make a difference though, Commented Jun 13, 2011 at 1:22
  • nothing from your description seems wrong to me. Commented Jun 13, 2011 at 1:22
  • @Sai, he has auto flush enabled. Commented Jun 13, 2011 at 1:23
  • I've tried pw.flush() but it didn't work. The code is long. I'm not sure how it will help. Commented Jun 13, 2011 at 1:24
  • Can't think of anything else without looking at the code. I assume no exceptions or errors are being thrown in the code. Commented Jun 13, 2011 at 1:32

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

hm ... this is a queer problem that you're having. Care to share what OS? JDK Version? Target platform version? You are using? Maybe there might be some quirks or bug. Also, care to share the segment of the code you are having problems so that many pairs of eyes can take a look at it?
java version "1.6.0_16" Java(TM) SE Runtime Environment (build 1.6.0_16-b01) Java HotSpot(TM) 64-Bit Server VM (build 14.2-b01, mixed mode), Linux tui 2.6.28-15-generic #52-Ubuntu SMP Wed Sep 9 10:48:52 UTC 2009 x86_64 GNU/Linux
Looks good to me. That's about the same build that I have and I don't have an issue or bug like you've reported. Care to share the code so we can help?
0

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

3 Comments

I've already tried pw.flush(). I have also set autoFlush=true in the PrinterWriter constructor
what I don't understand is why only the first 10% is not written and the rest is. In the console the first 10% is printed though :/
this seem pretty odd, perhaps show the fullcode? (java2s.com/Tutorial/Java/0180__File/…) it's rather straightforward with the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.