Search code examples
javaiobufferedwriterwriter

What is the consequence of not closing a BufferedWriter


Suppose I have a BufferedWriter that has written 5000 rows (some of which are not yet flushed).

What are the consequences if I do not close() this instance at the end of my program?


Solution

  • If you don't close() it before your program ends, buffered data that hasn't been written yet will never be written.

    BufferedWriter does all of its buffering in Java land (as opposed to relying solely on the OS's lower level buffers). Therefore it takes an explicit action to flush the buffers and close the files, as this does not happen automatically when your program exits (due primarily to the GC'd nature of Java and the lack of guarantees on when and if, e.g., finalize() is called).

    So anything in the BufferedWriter's buffers that's still there when the program exits ends up getting lost forever.

    You should make it a habit of close()ing streams when you are done with them (and nulling any references out to them as well so the GC can discard the object; this doesn't have any impact on BufferedWriters behavior, but it is a good general practice). At the bare minimum, if you've written important data and you need to ensure it has been written to disk whether you need to close the stream or not, you can explicitly call flush() as needed.

    The source code for BufferedWriter can be found here, if you'd like to take a look at the implementation.