Is FileChannel#force is equivalent to OutputStream#flush? Do I always have to call it?

前端 未结 1 660
别跟我提以往
别跟我提以往 2021-01-27 11:21

I have a class works like FilterOutputStream.

public class WritableFilterChannel implements WritableChannel         


        
相关标签:
1条回答
  • 2021-01-27 11:54

    "Equivalent" is too strong a word. FileChannel.force(false) is similar to OutputStream.flush(). FileChannel's force() method offers stronger guarantees about the state of the file after it returns than OutputStream's flush() method.

    Obviously you do not have to close() the FileChannel that you called the force() method on. You should only close the channel when you have finished with it. However, there is no guarantee that closing the channel will cause the equivalent of a force operation on it. If you need the behavior that force() specifies as part of the channel closure then you must explicitly call it the way you are doing in your close() method.

    0 讨论(0)
提交回复
热议问题