• 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

Have to call flush() or close()?

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which File IO objects require you to call flush() or close() in order to have output written to the underlying file?
I had a question where a FileOutputStream was chained to a DataOutputStream and then the DataOutputStream wrote to the underlying stream. In the question they never called flush() or close() so I thought nothing would be written, but apparently flush() or close() need not be called to ensure writing to the file takes place? I tested the answer and sure enough writing did take place.
So now I'm confused. Which file IO operations "require" flush() or close() to be called in order to ensure bytes get written to the underlying stream and file? I thought they all did (except for PrintWriter can take the boolean autoflush as an arg ).
TIA.
Rick
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everytime you buffer your output streams (BufferedOutputStream, BufferedWriter) you should invoke flush to ensure that what you feed into the write method of the stream/writer is actually written for good.
Moreover, as a matter of good style and practice you should always invoke flush and close before getting rid of your stream object.
HIH
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic