Hi, First post - great set of forums! Right - down to business... The 1.3.1 javadoc for BufferedWriter says that the close() method "closes the stream", It also says that the close method "overrides close() in class Writer". Now if you look at the close() method in class Writer, the javadoc says "closes the stream, flushing it first". So you can see where I'm going with this... From the javadoc, I'd kind of expect that a close() on a BufferedWriter would not automtically do a flush() first - but it does.
first, Writer is an abstract class and close() is abstract, so that method in Writer has no implementation. Second, if you look at the source for BufferedWriter (here in Java 1.4.2_04)
you can see invoking BufferedWriter.close() purges the internal buffer but does not flush the associated output stream before closing it. You still need to invoke BufferedWriter.flush() before BufferedWriter.close() to insure your data gets to where it's going.
[JC]: Now if you look at the close() method in class Writer, the javadoc says "closes the stream, flushing it first". This is all you need, really. Any Writer is expected to flush its contents as part of the close() mehtod. If it doesn't, it violates its API. [JC]: The 1.3.1 javadoc for BufferedWriter says that the close() method "closes the stream", It also says that the close method "overrides close() in class Writer". Yes, but that doesn't contradict the former comment. "Closes the stream" implies flushing it first, according to the API for any Writer. [JE]: you can see invoking BufferedWriter.close() purges the internal buffer but does not flush the associated output stream before closing it. But the "associated output stream" is actually another Writer. Which means that closing the stream will also flush it. Problem solved. In contrast, the OutputStream class does not specify that a close() will flush the stream. The FilterOutputStream does so, though subclasses are allowed to implement differently if they wish. But a BufferedOutputStream does not override close(), so it inherits from FilterOutputStream, and thus it flushes before it closes. PrintStream in contrast does override FilterOutputStream's close(), but the API for the overriding method explicitly says it flushes as part of close(). So my impression is that for most existing OutputStream classes flushing isn't really an issue as long as you close - however you should be careful; it's not guaranteed for OutputStreams in general. Which is annoying. My guess is that this was simply an oversight; OutputStream was part of JDK 1.0, and there were a lot of details that were kind of rushed back then. Writer came out in JDK 1.1, when they'd had a little more time to think up a good API. But they may not have felt they could change the API to OutputStream at that point, since that could break backward compatibility for existing classes (e.g. third-party libraries) obeying the original OutputStream API without flushing. Too bad.
"I'm not back." - Bill Harding, Twister
subject: Buffered Writer automatically does flush() on a close()?