Skip to main content
added 390 characters in body
Source Link
Joachim Sauer
  • 309.3k
  • 59
  • 568
  • 624

Bad things that can happen when you don't close your streams:

  • you can run out of file handles
  • data that you think is written to disk may still be in the buffer (only)
  • files might still be locked for other processes (depends on the platform)
  • ...

Yes, close operation always flushes the stream.

Yes, all streamsAll file handles that the OS is aware of are closed when. This means effectively that FileOutputStream, FileInputStream and the application quitsinput/output of a Socket will be closed. But if you wrap a FileOutputStream in a BufferedOutputStream then that BufferedOutputStream will not be known to the OS and won't be closed/flushed on shutdown. So data written to the BufferedOutputStream but not yet flushed to the FileOutputStream can be lost.

Bad things that can happen when you don't close your streams:

  • you can run out of file handles
  • data that you think is written to disk may still be in the buffer (only)
  • files might still be locked for other processes (depends on the platform)
  • ...

Yes, close operation always flushes the stream.

Yes, all streams are closed when the application quits.

Bad things that can happen when you don't close your streams:

  • you can run out of file handles
  • data that you think is written to disk may still be in the buffer (only)
  • files might still be locked for other processes (depends on the platform)
  • ...

Yes, close operation always flushes the stream.

All file handles that the OS is aware of are closed. This means effectively that FileOutputStream, FileInputStream and the input/output of a Socket will be closed. But if you wrap a FileOutputStream in a BufferedOutputStream then that BufferedOutputStream will not be known to the OS and won't be closed/flushed on shutdown. So data written to the BufferedOutputStream but not yet flushed to the FileOutputStream can be lost.

Source Link
Joachim Sauer
  • 309.3k
  • 59
  • 568
  • 624

Bad things that can happen when you don't close your streams:

  • you can run out of file handles
  • data that you think is written to disk may still be in the buffer (only)
  • files might still be locked for other processes (depends on the platform)
  • ...

Yes, close operation always flushes the stream.

Yes, all streams are closed when the application quits.