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 ]
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.
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
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.