Are FileChannel.force and FileDescriptor.sync both needed?

前端 未结 1 1332
借酒劲吻你
借酒劲吻你 2021-01-17 22:26

In https://stackoverflow.com/questions/730521/really-force-file-sync-flush-in-java, the author writes in the summary of the answers:

Use c.force(tru

相关标签:
1条回答
  • 2021-01-17 23:18

    My understanding is that the correct answer is No.

    FileChannel.force calls fdatasync or fsync. This can be seen in jdk/src/solaris/native/sun/nio/ch/FileChannelImpl.c on of the OpenJDK source code. FileDescriptor calls fsync (To find this out was more complex. I finally traced it to jvm.cpp).

    I am the "author" of the linked question. So I was wrong. What is NOT enough is FileOutputStream.flush. because it is a no-op. You therefore either need force or sync.

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