简体   繁体   English

RandomAccessFile.close()是否在内部调用FileChannel.force()?

[英]Does RandomAccessFile.close() internally call FileChannel.force()?

I am using RandomAccessFile to perform some writes to a file as part of a transaction. 我正在使用RandomAccessFile作为事务的一部分对文件执行一些写操作。 Before I commit my transaction, I want to be absolutely sure that the data is written to disk. 在提交事务之前,我要绝对确定将数据写入磁盘。 Calling force(boolean) on the RAF's FileChannel appears to provide this guarantee, but is it called implicitly when I close the file, or do I have to call it manually? 在RAF的FileChannel上调用force(boolean)似乎提供了这种保证,但是当我关闭文件时是否隐式调用它,还是必须手动调用它?

Also, does anybody have any insight into what force() actually does, and how far it can be trusted? 此外,是否有人对force()实际force()有任何见识,以及它可以被信任的程度? Is it possible that the OS may report that the data has been written to disk, when in fact it is still sitting in some cache somewhere? 实际上,OS仍坐在某个缓存中时,操作系统是否可能报告已将数据写入磁盘? To what extent is this OS/HDD/filesystem-dependent? 此OS / HDD /文件系统在多大程度上依赖于?

This is completely FS-dependent. 这完全取决于FS。 But if you do not get an (IO)Exception, you should trust the JVM and commit your transaction. 但是,如果未收到(IO)Exception,则应信任JVM并提交事务。 Whether you can trust your FS is the other question. 您是否可以信任自己的FS是另一个问题。 For example, if you use ext4 having delayed allocation activated, you might lose data on a instant power loss even after RandomAccessFile#force(boolean) returned successfully. 例如,如果您使用激活了延迟分配的ext4,则即使在RandomAccessFile#force(boolean)成功返回后,您也可能因瞬间断电而丢失数据。 The JVM relies on the FS-API of your operating system and does not care about the actual implementation and configuration. JVM依赖于操作系统的FS-API,并不关心实际的实现和配置。

And yes, you have to call RandomAccessFile#force(boolean) before closing. 是的,您必须在关闭之前调用RandomAccessFile#force(boolean)。 Whether #close invokes #force is up to the FileChannel's implementation. #close是否调用#force取决于FileChannel的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM