简体   繁体   English

使用 try-with-resources 时是否需要调用 flush()

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

Will try-with-resources call flush() implicitly? try-with-resources隐式调用flush()吗?

If it does, in the following code snippet, bw.flush() can be safely removed?如果是这样,在下面的代码片段中,可以安全地删除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.附: I don't see any description about it in official document:我在官方文档中没有看到任何关于它的描述:

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html https://docs.oracle.com/javase/8/docs/api/java/lang/Au​​toCloseable.html

Closeable and AutoCloseable are general-purpose interfaces that do not know anything about flushing. CloseableAutoCloseable是通用接口,对刷新一无所知。 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.另一方面, Writer是一个更具体用途的抽象类,现在知道一些有关刷新的知识。 Some excerpt of the documentation for the method Writer.close() : Writer.close()方法的一些文档摘录:

Closes the stream, flushing it first .关闭流,首先刷新它

So - yes - when using a writer, a close will always also flush .所以 - 是的 - 使用 writer 时, close也总是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.使用try-with-resource块时,资源会自动关闭。 As part of this process it will also invoke flush automatically.作为此过程的一部分,它还将自动调用刷新。

As mentioned in doc for close method of BufferedWriter:正如文档中提到的 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.一旦流关闭,进一步的 write() 或 flush() 调用将导致抛出 IOException。

Quoting javadoc of BufferedWriter.close() :引用BufferedWriter.close() 的javadoc :

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:因此,您不需要显式调用flush方法,因为它会被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.一旦流关闭,进一步的 write() 或 flush() 调用将导致抛出 IOException。 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 .这种行为从继承作家班,因此除了BufferedWriter将通过设置也相同的行为: 的CharArrayWriterFilterWriterOutputStreamWriterPipedWriter为PrintWriterStringWriter的

This behavior is not provided in the documentation of the tryWithResources or AutoCloseable as the behavior is specific to the given implementation of Writer . tryWithResourcesAutoCloseable的文档中未提供此行为,因为该行为特定于Writer的给定实现。 As Writer extends Closeable , it will call the close method when exiting the try {} block and the close method will first call flush as already mentioned.由于Writer扩展了Closeable ,它会在退出 try {} 块时调用close方法,并且 close 方法将首先调用前面提到的flush

I really don't understand why other answers focus on the BufferedWriter not try-with-resources .我真的不明白为什么其他答案关注BufferedWriter而不是try-with-resources

I, either, couldn't find any specification or mentions that the try-with-resources statements calls flush() on any objects of Flushable .我要么找不到任何规范,要么提到try-with-resources语句对Flushable任何对象调用flush()

https://docs.oracle.com/javase/specs/jls/se13/html/jls-14.html#jls-14.20.3 https://docs.oracle.com/javase/specs/jls/se13/html/jls-14.html#jls-14.20.3

Don't rely on any undocumented/unspecified behavior of vendor specific implementations.不要依赖供应商特定实现的任何未记录/未指定的行为。

try (OutputStream o = open()) {
    //writeSome
    o.flush(); // won't hurt!
}

From the Javdocs:来自 Javdocs:

The try-with-resources statement is a try statement that declares one or more resources. try-with-resources 语句是一种声明一个或多个资源的 try 语句。 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. try-with-resources 语句确保每个资源在语句结束时关闭。 Any object that implements java.lang.AutoCloseable , which includes all objects which implement java.io.Closeable , can be used as a resource.任何实现java.lang.AutoCloseable对象,包括实现java.io.Closeable所有对象,都可以用作资源。

The BufferedWriter.close() explicitly stated that: 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.一旦流关闭,进一步的 write() 或 flush() 调用将导致抛出 IOException。 Closing a previously closed stream has no effect.关闭先前关闭的流没有任何效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM