开发者

Are FileChannel.force and FileDescriptor.sync both needed?

开发者 https://www.devze.com 2023-02-25 10:51 出处:网络
Inhttps://stackoverflow.com/questions/730521/really-force-file-sync-flush-in-java, the author writes in the summary of the answers:

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(true) followed by s.getFD().sync() for Java NIO

My question is: do you really need both? Isn't force enough? Aren't force and sync just different interfaces for doing the same thing? I can't find any开发者_Go百科place where this is confirmed.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号