问题描述
如果我只是在输出流中调用close()
,输出是有保证的,还是需要我总是调用flush()
?
If I just call close()
in a output stream, the output is guaranteed, or need I call flush()
always?
推荐答案
Close() 总是刷新所以不需要调用.
Close() always flushes so no need to call.
这个答案基于常识和我遇到的所有输出流.谁将在不先刷新缓冲区的情况下为缓冲流实现 close() ?在 close() 之前调用 flush 没有害处.但是,如果过度调用flush() 会产生后果.它可能会破坏缓冲机制.
This answer is based on common sense and all the outputstream I encountered. Who is going to implement a close() for a buffered stream without flushing buffer first? There is no harm to call flush right before close(). However, there are consequences if flush() is called excessively. It may defeat underneath buffering mechanism.
这篇关于在 Java 中,当我调用 OutputStream.close() 时,是否总是需要先调用 OutputStream.flush()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!