This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Buffered Writer automatically does flush() on a close()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Buffered Writer automatically does flush() on a close()?" Watch "Buffered Writer automatically does flush() on a close()?" New topic
Author

Buffered Writer automatically does flush() on a close()?

Julian Corallo
Greenhorn

Joined: Mar 26, 2004
Posts: 8
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.

Cheers,
Jules
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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
 
Consider Paul's rocket mass heater.
 
subject: Buffered Writer automatically does flush() on a close()?
 
Similar Threads
Last line not being written to the file on using BufferedWriter
Function of close() and flush() methods..
Procuing Output to a text file is showing peculier behaviour
Do we need to close FileWriter Object???
HELP!!! Output stream closing prematurely