使用 try-with-resources 时是否需要调用 flush()
Is flush() call necessary when using try-with-resources
try-with-resources
会隐式调用 flush()
吗?
如果是,在下面的代码片段中,bw.flush()
可以安全删除吗?
static void printToFile1(String text, File file) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
bw.write(text);
bw.flush();
} catch (IOException ex) {
// handle ex
}
}
ps。
我在官方文档中没有看到任何关于它的描述:
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
使用try-with-resource块时资源自动关闭。作为此过程的一部分,它还将自动调用刷新。
正如文档中提到的 BufferedWriter 的 close 方法:
Closes the stream, flushing it first. Once the stream has been closed,
further write() or flush() invocations will cause an IOException to be
thrown.
引用 BufferedWriter.close() 的 javadoc:
Closes the stream, flushing it first.
来自 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.
BufferedWriter.close()
明确指出:
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.
Closeable
and AutoCloseable
是对刷新一无所知的通用接口。所以你在他们的文档中找不到任何关于它的信息——除了一些关于释放资源.
的词
一个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.
所以 - 是的 - 当使用 writer 时,close
也总是 flush
。这基本上意味着您在尝试找出关闭的真正作用时必须查阅您正在使用的具体文档 类。
本例最少要写的代码量:
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
bw.write("Test");
} catch (IOException e) {
// handle exception
}
因此您不需要显式调用 flush method, as it will be called by the close 方法,如 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.
此行为继承自 Writer class, hence besides BufferedWriter the same behavior is provided also by: CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter。
退出try{}块时tryWithResources or AutoCloseable as the behavior is specific to the given implementation of Writer. As Writerextends Closeable, it will call the close方法的文档中没有提供此行为,close方法将首先调用
flush 如前所述。
我真的不明白为什么其他答案关注 BufferedWriter
而不是 try-with-resources
。
我也找不到任何规范或提到 try-with-resources
语句对 Flushable
的任何对象调用 flush()
。
https://docs.oracle.com/javase/specs/jls/se13/html/jls-14.html#jls-14.20.3
不要依赖供应商特定实现的任何 undocumented/unspecified 行为。
try (OutputStream o = open()) {
//writeSome
o.flush(); // won't hurt!
}
try-with-resources
会隐式调用 flush()
吗?
如果是,在下面的代码片段中,bw.flush()
可以安全删除吗?
static void printToFile1(String text, File file) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
bw.write(text);
bw.flush();
} catch (IOException ex) {
// handle ex
}
}
ps。 我在官方文档中没有看到任何关于它的描述:
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
使用try-with-resource块时资源自动关闭。作为此过程的一部分,它还将自动调用刷新。
正如文档中提到的 BufferedWriter 的 close 方法:
Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown.
引用 BufferedWriter.close() 的 javadoc:
Closes the stream, flushing it first.
来自 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 implementjava.io.Closeable
, can be used as a resource.
BufferedWriter.close()
明确指出:
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.
Closeable
and AutoCloseable
是对刷新一无所知的通用接口。所以你在他们的文档中找不到任何关于它的信息——除了一些关于释放资源.
一个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.
所以 - 是的 - 当使用 writer 时,close
也总是 flush
。这基本上意味着您在尝试找出关闭的真正作用时必须查阅您正在使用的具体文档 类。
本例最少要写的代码量:
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
bw.write("Test");
} catch (IOException e) {
// handle exception
}
因此您不需要显式调用 flush method, as it will be called by the close 方法,如 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.
此行为继承自 Writer class, hence besides BufferedWriter the same behavior is provided also by: CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter。
退出try{}块时tryWithResources or AutoCloseable as the behavior is specific to the given implementation of Writer. As Writerextends Closeable, it will call the close方法的文档中没有提供此行为,close方法将首先调用 flush 如前所述。
我真的不明白为什么其他答案关注 BufferedWriter
而不是 try-with-resources
。
我也找不到任何规范或提到 try-with-resources
语句对 Flushable
的任何对象调用 flush()
。
https://docs.oracle.com/javase/specs/jls/se13/html/jls-14.html#jls-14.20.3
不要依赖供应商特定实现的任何 undocumented/unspecified 行为。
try (OutputStream o = open()) {
//writeSome
o.flush(); // won't hurt!
}