Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

flushes on close  RSS feed

 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hopefully straight forward question ...

I don't do that much Java I/O, I'm reading a book that takes a paragraph to emphasis that flush should be called before close to make sure all bytes are written with a FileWriter example (which might make sense), however FileWriter is an OutputStreamWriter is a Writer and the sun close documentation (1.4 & 1.5 ) pushes me towards the writer close documentation etc, which has the following comment ...

Close the stream, flushing it first. i.e. flush not required.

so my question is , is the book wrong, the documentation wrong, or my interpretation of the issue\ reading of the comment etc. wrong

thanks

Chris
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15860
80
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing is wrong. The close() method of class Writer is just being "nice" to you - it calls flush() for you so that you don't have to, and so that you don't get into trouble when you forget.

In general with streams however, flush() is not called automatically when you close an OutputStream.

Note the difference between InputStream / OutputStream, which are low-level classes to read and write data, and Reader / Writer, which are higher-level classes that work on top of streams to read and write text (and do the necessary character set conversions).
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my point was the book wrote an example using that class, calling flush when that seems to be in this case unecessary and then goes on to writes a paragraph on how necessary the flush is, which made me think I was reading the documentation wrong .

Its an SCJP 1.5 book , I already have a 1.4 but IO came in with the 1.5 exam so I'm brushing up and this is the kind of thing they ask about so unfortunately I do need a definitive answer i.e. if it was just in some ones source code I was using I would ignore the flush being necessary or not as having it won't cause any harm and not give it a second thought.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris, I would agree that the book is simply wrong in this case - you should never need to call flush() before close() for any Writer. Sadly you do need to do it for an OutputStream, in general, but not for a Writer. Some OutputStream implementations will call flush() for you as part of close(), but it's not guaranteed - and in particular BufferedOutputStream does not call flush() from within close(). Personally I consider this a rather poor design choice, but it's one they made long ago - and technically it could break backward compatibility for them to change the API for close() now, so we're stuck with it now.

As for the SCJP, I think it's exceedingly unlikely you'd be asked to know about this subtle difference between the OutputStream and Writer APIs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!