In C, when I call open()
to open a file descriptor, I have to explicitly pass the O_SYNC
flag to ensure that writes to this file will be persisted to disk by the time write()
returns. If I want to, I can not supply O_SYNC
to open()
, and then my writes will return much more quickly because they only have to make it into a filesystem cache before returning. If I want to, later on I can force all outstanding writes to this file to be written to disk by calling fsync()
, which blocks until that operation has finished. (More details are available on all this in the Linux man page.)
Is there any way to do this in Java? The most similar thing I could find was using a BufferedOutputStream
and calling .flush()
on it, but if I'm doing writes to randomized file offsets I believe this would mean the internal buffer for the output stream could end up consuming a lot of memory.