在使用try-with-resources时,是否需要调用flush()方法?

6 浏览
0 Comments

在使用try-with-resources时,是否需要调用flush()方法?

在使用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) {
        // 处理异常
    }
}

附注:

我在官方文档中没有找到相关描述:

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html

0