简体   繁体   English

如何刷新'RandomAccessFile'(java)?

[英]How do I flush a 'RandomAccessFile' (java)?

I'm using RandomAccessFile in java: 我在java中使用RandomAccessFile:

file = new RandomAccessFile(filename, "rw");
...
file.writeBytes(...);

How can I ensure that this data is flushed to the Operating System? 如何确保将此数据刷新到操作系统? There is no file.flush() method. 没有file.flush()方法。 (Note that I don't actually expect it to be physically written, I'm content with it being flushed to the operating system, so that the data will survive a tomcat crash but not necessarily an unexpected server power loss). (请注意,我实际上并不期望它是物理写入的,我很满意它被刷新到操作系统,因此数据将在tomcat崩溃中存活,但不一定是意外的服务器断电)。

I'm using tomcat6 on Linux. 我在Linux上使用tomcat6。

The only classes that provide a .flush() method are those that actually maintain their own buffers. 提供.flush()方法的唯一类是那些实际维护自己的缓冲区的类。 As java.io.RandomAccessFile does not itself maintain a buffer, it does not need to be flushed. 由于java.io.RandomAccessFile本身不维护缓冲区,因此不需要刷新它。

Have a look carefully at RandomAccessFile constructor javadoc: 仔细看看RandomAccessFile构造函数javadoc:

The "rws" and "rwd" modes work much like the force(boolean) method of the FileChannel class, passing arguments of true and false, respectively, except that they always apply to every I/O operation and are therefore often more efficient. “rws”和“rwd”模式的工作方式与FileChannel类的force(boolean)方法非常相似,分别传递true和false参数,但它们始终适用于每个I / O操作,因此通常更有效。 If the file resides on a local storage device then when an invocation of a method of this class returns it is guaranteed that all changes made to the file by that invocation will have been written to that device . 如果文件驻留在本地存储设备上,那么当调用此类的方法返回时,可以保证该调用对该文件所做的所有更改都将写入该设备 This is useful for ensuring that critical information is not lost in the event of a system crash. 这对于确保在系统崩溃时不会丢失关键信息非常有用。 If the file does not reside on a local device then no such guarantee is made. 如果文件不驻留在本地设备上,则不会进行此类保证。

您可以使用getFD().sync()方法。

here's what i do in my app: 这是我在我的应用程序中所做的:

rf.close();
rf = new RandomAccessFile("mydata", "rw");

this is give 3-4times gain in performance compared to getFd().sync() and 5-7 times compared to "rws' mode 与getFd()。sync()相比,性能提高3-4倍,与“rws”模式相比提高5-7倍

deoes exactly what the original question proposed: passes on unsaved data to the OS and out of JVM. 完全解决原始问题提出的问题:将未保存的数据传递给操作系统和JVM。 Doesn't physically write to disk, and therefore introduces no annoying delays 不物理写入磁盘,因此不会引入烦人的延迟

I reached here with the very same curiosity. 我带着同样的好奇心来到这里。

And I really can't figure what need to flush on OS and not necessarily need to flush to Disk part means. 我真的无法想象需要在操作系统上刷新什么,而不一定需要刷新到磁盘部件意味着。

In my opinion, 在我看来,

The best thing matches to the concept of a managed flushing is getFD().sync() , as @AVD said, 托管刷新概念相匹配的最好的事情是getFD().sync() ,正如@AVD所说,

try(RandomAccessFile raw = new RandomAccessFile(file, "rw")) {
    raw.write...
    raw.write...
    raw.getFD().sync();
    raw.wirte...
}

which looks like, by its documentation, it works very much like what FileChannel#force(boolean) does with true . 看起来,通过它的文档,它的工作方式非常类似于FileChannel #force(boolean)true

Now "rws" and "rwd" are look like they work as if specifying StandardOpenOption#SYNC and StandardOpenOption#DSYNC respectively while a FileChannel is open . 现在, "rws""rwd"看起来好像在FileChannel open分别指定StandardOpenOption#SYNCStandardOpenOption#DSYNC

try(RandomAccessFile raw = new RandomAccessFile(file, "rws")) {
    raw.write...
    raw.write...
    raw.wirte...
    // don't worry be happy, woo~ hoo~ hoo~
}

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

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