Java:RandomAccessFile 模式“rws”与“rwd”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14232539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: RandomAccessFile Mode "rws" vs "rwd"?
提问by Cristian Diaconescu
The RandomAccessFile
constructoraccepts a mode
string specifying how a file should be open.
该RandomAccessFile
构造函数接受一个mode
字符串,指定文件应该如何打开。
I'm confused about the difference between "rws"
and "rwd"
modes.
我对"rws"
和"rwd"
模式之间的区别感到困惑。
Here's what the docsstate:
以下是文档的说明:
"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadatabe written synchronously to the underlying storage device.
"rwd" Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.
[...]
The "rwd" mode can be used to reduce the number of I/O operations performed. Using "rwd" only requires updates to the file's content to be written to storage; using "rws" requires updates to both the file's content and its metadata to be written, which generally requires at least one more low-level I/O operation.
"rws" 与 "rw" 一样,为读取和写入打开,并且还要求对文件内容或元数据的每次更新都同步写入底层存储设备。
"rwd" 与 "rw" 一样用于读写,并且还要求对文件内容的每次更新都同步写入底层存储设备。
[...]
“rwd”模式可用于减少执行的 I/O 操作的数量。使用“rwd”只需要更新要写入存储的文件内容;使用“rws”需要更新要写入的文件内容及其元数据,这通常至少需要再进行一次低级 I/O 操作。
...and no explanation about what metadata
means. Does it mean that "rws"
updates the last modified timestampon the filesystem, and "rwd"
doesn't ?
......并且没有解释什么metadata
意思。这是否意味着"rws"
更新文件系统上最后修改的时间戳,而"rwd"
不是?
回答by Peter Lawrey
Does it mean that "rws" updates the last modified timestamp on the filesystem, and "rwd" doesn't ?
这是否意味着“rws”更新文件系统上最后修改的时间戳,而“rwd”不更新?
rws flushes the contents of the file and the modification date of the file.
rws 刷新文件的内容和文件的修改日期。
rwd flushs the contents of the file, but the modification date might not change until the file is closed.
rwd 刷新文件的内容,但在文件关闭之前修改日期可能不会更改。
rw only flushes when you tell it to and doesn't change the modifcation date until you close the file.
rw 仅在您告诉它时刷新,并且在您关闭文件之前不会更改修改日期。
BTW rwd is much slower for writes than rw, and rws is slower again.
顺便说一句,rwd 的写入速度比 rw 慢得多,而 rws 又慢得多。
回答by Evgeniy Dorofeev
There is some info about file metadata in FileChannel API http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html
FileChannel API http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html 中有一些关于文件元数据的信息
...The file may also have some associated metadata such as access permissions, content type, and last-modification time...
Besides, FileChannel.force(boolean metadata)
API provides more info about the difference between rws and rwd (though the names are never mentioned)
此外,FileChannel.force(boolean metadata)
API 提供了更多关于 rws 和 rwd 之间区别的信息(尽管从未提及名称)