Java: RandomAccessFile Mode "rws" vs "rwd"?

Cristian Diaconescu picture Cristian Diaconescu · Jan 9, 2013 · Viewed 13.6k times · Source

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 ?

Answer

Peter Lawrey picture Peter Lawrey · Jan 9, 2013

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.