In Java, when I call OutputStream.close() do I always need to call OutputStream.flush() before?

前端 未结 4 1146
温柔的废话
温柔的废话 2020-11-27 19:46

If I just call close() in a output stream, the output is guaranteed, or need I call flush() always?

4条回答
  •  误落风尘
    2020-11-27 20:04

    Close() always flushes so no need to call.

    EDIT: 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.

提交回复
热议问题