File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Is my buffer messed up? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is my buffer messed up?" Watch "Is my buffer messed up?" New topic
Author

Is my buffer messed up?

Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
I have the code I will paste below. In it I have various print statements that print to System.out and to a file. Printing to System.out always works, so I know that the code is doing what it's supposed to do but for some reason I can't always get it to print to the file. I put in duplicate print statements right next to one another, one printing to System.out and one printing to the file, with buffer flushes surrounding them. So I know that the buffer is not full. In the file it prints the list of averages as well as "sortedMap.size is 52" from the second method but, "Sorted Averages, Indices" is only ever printed to System.out. What the heck?
Thanks for any ideas, Rebecca


[ EJFH: Added "CODE" tags so code formatting shows. ]
[ November 11, 2004: Message edited by: Ernest Friedman-Hill ]

SCJP 1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

After the line

out.println("\n Sorted Averages, Indices \n\n");

I see no out.flush() calls, so indeed, the data is being held up in a buffer. But perhaps more seriously, and to the point, you don't ever close "out". If you closed the file at the end, all the output would be flushed automatically. It's often nice call close() in a finally block, like



so that close() is called regardless of previous I/O errors.


[Jess in Action][AskingGoodQuestions]
Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
Hey, thanks. That fixed it. I didn't realize that you had to flush a buffer to be certain you got it to print whatever was in it. I thought flushing was just sort of a way to get it unclogged if it was trying to hold too much stuff at once before writing the stuff to a file. You guys are a big help and allow me not to irritate my more knowledgable coworkers nearly as much. :-)
Rebecca
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rebecca Witmer:
I thought flushing was just sort of a way to get it unclogged if it was trying to hold too much stuff at once before writing the stuff to a file.
The buffer will flush() itself for vaious reasons, most notably when it gets full. You can call flush() yourself if you want whatever might still be buffered to be written immediately. Finally, closing the stream flushes the buffer.

To clarify then, you never need to call flush(), but you may. Usually you would do this if some other process is reading from the file/socket to which you're writing and you want to make sure they aren't waiting for your buffer to flush() itself.
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Note that in the case of you never need to call flush().

That stream is not buffered.

Guy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is my buffer messed up?