问题描述
根据java文档,在任何java.io Streams上调用close()会自动调用flush()。但是我已经看到很多例子,即使在生产代码中,开发人员也在close()之前明确使用了flush()。在什么条件下我们需要在close()之前使用flush()?
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()?
推荐答案
开发人员养成调用flush的习惯(写完一些必须发送的内容之后。
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.
// write a message
out.write(buffer, 0, size);
out.flush();
// finished
out.close();
正如您所看到的,flush()是多余的,但意味着您正在遵循模式。
As you can see the flush() is redundant, but means you are following a pattern.
这篇关于在close()之前使用flush()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!