• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic