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