Java 在 close() 之前使用 flush()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9858495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:23:28  来源:igfitidea点击:

Using flush() before close()

javaiostream

提问by Sivasubramaniam Arunachalam

As per the java docs, invoking close() on any java.io Streams automatically invokes flush(). But I have seen in lot of examples, even in production codes, developers have explicitly used flush() just before close(). In what conditions we need to use flush() just before close()?

根据 java 文档,在任何 java.io Streams 上调用 close() 都会自动调用 flush()。但是我在很多例子中看到,甚至在生产代码中,开发人员在 close() 之前明确使用了 flush()。在什么情况下我们需要在 close() 之前使用flush()?

采纳答案by Peter Lawrey

Developer get into a habit of calling flush() after writing something which must be sent.

开发人员养成了在编写必须发送的内容后调用 flush() 的习惯。

IMHO Using flush() then close() is common when there has just been a write e.g.

恕我直言,当刚刚写入时,使用 flush() 然后 close() 很常见,例如

// write a message
out.write(buffer, 0, size);
out.flush();

// finished
out.close();

As you can see the flush() is redundant, but means you are following a pattern.

如您所见,flush() 是多余的,但意味着您遵循一种模式。

回答by Bozho

I guess in many cases it's because they don't know close()also invokes flush(), so they want to be safe.

我想在很多情况下是因为他们不知道close()也 invokes flush(),所以他们想要安全。

Anyway, using a buffered stream should make manual flushing almost redundant.

无论如何,使用缓冲流应该使手动刷新几乎是多余的。

回答by Fabian

I want to point out an important concept that many previous comments have alluded to:

我想指出一个重要的概念,许多以前的评论都提到过:

A stream's close()method does NOT necessarilyinvoke flush().

流的close()方法不一定调用flush().

For example org.apache.axis.utils.ByteArray#close() does not invoke flush().
(click link to see source code)

例如org.apache.axis.utils.ByteArray#close() 不会调用flush().
(点击链接查看源代码)

The same is true more generally for any implementations of Flushableand Closeable. A prominent example being java.io.PrintWriter. Its close()method does NOT call flush().
(click link to see source code)

这同样适用一般多为任何实现FlushableCloseable。一个突出的例子是java.io.PrintWriter。它的close()方法不会调用flush().
(点击链接查看源代码)

This might explain why developers are cautiously calling flush()before closing their streams. I personally have encountered production bugs in which close()was called on a PrintWriter instance without first calling flush().

这可能解释了为什么开发人员flush()在关闭他们的流之前谨慎地调用。我个人遇到过生产错误,其中close()在没有首先调用flush().

回答by stackoverflowed

The answers already provided give interesting insights that I will try to compile here.

已经提供的答案提供了有趣的见解,我将在这里尝试编译。

Closeableand Flushablebeing two independent traits, Closeabledo not specify that close()should call flush(). This means that it is up to the implementation's documentation (or code) to specify whether flush()is called or not. In most cases it is the norm, but there is no guaranty.

Closeable并且Flushable作为两个独立的特征,Closeable不指定close()应调用flush(). 这意味着由实现的文档(或代码)来指定是否flush()调用。在大多数情况下,这是常态,但没有保证。

Now regarding what @Fabian wrote: It is true that java.io.PrintWriter's close()method does not call flush(). However it calls out.close()(outbeing the underlying writer). Assuming outis a BufferedWriter, we are fine since BufferedWriter.close()is flushing (according to it's doc). Had it be another writer, it may not have been the case...

现在关于@Fabian 所写的内容:java.io.PrintWriterclose()方法确实没有调用flush(). 但是它调用out.close()out作为底层作者)。假设out是 a BufferedWriter,我们很好,因为BufferedWriter.close()正在刷新(根据它的文档)。如果是另一位作家,情况可能并非如此......

So you have two choices:

所以你有两个选择:

  • either you ensure that at least one inner Writer/Stream flushes by itself (beware in case of code refactoring),
  • or you just call flush()and you're on the safe side all the time.
  • 要么确保至少有一个内部 Writer/Stream 自行刷新(注意代码重构),
  • 或者你只是打电话flush(),你就一直处于安全状态。

Solution 2, requiring less work, is my preferred one.

需要较少工作的解决方案 2 是我的首选。