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.

kkudi
  • 1,625
  • 4
  • 25
  • 47
  • Can you post some more of the code? Can you try a pw.flush() -- not sure if it will make a difference though, – Sai Jun 13 '11 at 01:22
  • nothing from your description seems wrong to me. – MeBigFatGuy Jun 13 '11 at 01:22
  • @Sai, he has auto flush enabled. – mre Jun 13 '11 at 01:23
  • I've tried pw.flush() but it didn't work. The code is long. I'm not sure how it will help. – kkudi Jun 13 '11 at 01:24
  • Can't think of anything else without looking at the code. I assume no exceptions or errors are being thrown in the code. – Sai Jun 13 '11 at 01:32
  • What I have figured out is that If I add a for loop and print 2000 lines, before my output, then I Get all my output. It's like there's some sort of boundary i need to go over before any text is shown. – kkudi Jun 13 '11 at 02:01

2 Answers2

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

Vern
  • 2,393
  • 1
  • 15
  • 18
  • 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? – Vern Jun 13 '11 at 01:37
  • 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 – kkudi Jun 13 '11 at 01:40
  • 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? – Vern Jun 13 '11 at 02:02
0

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

Jasonw
  • 5,054
  • 7
  • 43
  • 48
  • I've already tried pw.flush(). I have also set autoFlush=true in the PrinterWriter constructor – kkudi Jun 13 '11 at 01:24
  • 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 :/ – kkudi Jun 13 '11 at 01:25
  • this seem pretty odd, perhaps show the fullcode? (http://www.java2s.com/Tutorial/Java/0180__File/NestedcreationofFileWriterBufferedWriterandPrintWriter.htm) it's rather straightforward with the code. – Jasonw Jun 13 '11 at 01:37