15

The RandomAccessFile constructor accepts a mode string specifying how a file should be open.

I'm confused about the difference between "rws" and "rwd" modes.

Here's what the docs state:

"rws" Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be 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.

...and no explanation about what metadata means. Does it mean that "rws" updates the last modified timestamp on the filesystem, and "rwd" doesn't ?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233

2 Answers2

20

Does it mean that "rws" updates the last modified timestamp on the filesystem, and "rwd" doesn't ?

rws flushes the contents of the file and the modification date of the file.

rwd flushs the contents of the file, but the modification date might not change until the file is closed.

rw only flushes when you tell it to and doesn't change the modifcation date until you close the file.

BTW rwd is much slower for writes than rw, and rws is slower again.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • There is something I don't get: when you modify the contents of a file, ctime is modified in any case (at least with Unix{-like} systems)... I must be missing something here. – fge Jan 09 '13 at 10:16
  • 1
    @fge The hints define the minimum of what must happen as it is about providing guarentees. The OS is free to do more than this e.g. set the ctime for a new file. – Peter Lawrey Jan 09 '13 at 10:17
  • ctime is "content modification" time (mtime is metadata modification -- each time ctime is updated, mtime is also updated since ctime is part of the metadata) – fge Jan 09 '13 at 10:21
  • 2
    @fge When you change to the content, those changes are visible immediately even with "rw". I use this in a library I wrote to share data between processes and the latency can be as low at 100 ns. What it doesn't do is actually write the data to disk so even though it looks like the information has been changed the data is not yet on disk. Using "rws"/"rwd" ensures the data is written to disk before continuing e.g. to prevent data loss on an OS crash or power failure. – Peter Lawrey Jan 09 '13 at 10:24
  • Ah, yeah, OK, I missed the "synchronized" stuff part. Not that `fsync()` and friends make much of difference on most modern Unix OSes these days... – fge Jan 09 '13 at 10:26
  • 1
    @fge I can tell you it's allot slower. ;) – Peter Lawrey Jan 09 '13 at 10:27
  • Just to make sure Peter even if vm crushes the "rw" will persist changes right? only OS crush is loosing data... – vach Jul 12 '16 at 09:32
  • 1
    @PeterLawrey Excuse me, what does "flushes the contents of the file" mean ? Maybe you wanted to say "flushes the stream" ? – VanechikSpace May 28 '22 at 14:32
  • 1
    @VanechikSpace if you are using streams, yes. If you are using FileChannels or RandomAccessFile it is on each write. – Peter Lawrey May 29 '22 at 19:14
3

There is some info about file metadata in 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)

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Interesting, but I had to hunt for the info. The right URL is: http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#force(boolean) – Cristian Diaconescu Jan 09 '13 at 10:26