Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

It might not actually finish writing the data to wherever it's writing the data. So you'll lose whatever wasn't written yet.

The problem with a question like this is that it makes people wonder why you would ask. Are you trying to be a terrible person and not close your writers? Do you actually have a situation where closing the writer is something you don't want to do?

If you don't like closing streams, so use try-with-resource, new feature introduced in JDK 1.7, it will close all the such objects which implements Closable interface

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top