Is flush() call necessary when using try-with-resources

半腔热情 提交于 2019-11-30 08:12:10

Closeable and AutoCloseable are general-purpose interfaces that do not know anything about flushing. So you can't find any information about it in their documentation - except some words about releasing resources.

A Writer on the other hand is a more specific-purpose abstract class that now knows something about flushing. Some excerpt of the documentation for the method Writer.close():

Closes the stream, flushing it first.

So - yes - when using a writer, a close will always also flush. This basically means that you have to consult the documentation of the concrete classes that you are using when trying to find out what closing really does.

The resources are automatically closed when using try-with-resource block. As part of this process it will also invoke flush automatically.

As mentioned in doc for close method of BufferedWriter:

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown.

Quoting javadoc of BufferedWriter.close():

Closes the stream, flushing it first.

The minimum amount of code to be written in this case:

try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
    bw.write("Test");
} catch (IOException e) {
    // handle exception
}

Hence you don't need to call explicitly the flush method, as it will be called by the close method, as explained in the javadoc:

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

This behavior is inherited from the Writer class, hence besides BufferedWriter the same behavior is provided also by: CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter.

This behavior is not provided in the documentation of the tryWithResources or AutoCloseable as the behavior is specific to the given implementation of Writer. As Writerextends Closeable, it will call the close method when exiting the try {} block and the close method will first call flush as already mentioned.

Rahul Tripathi

From the Javdocs:

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

The BufferedWriter.close() explicitly stated that:

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!