There is StandardOpenOption.SYNC
and StandardOpenOption.DSYNC
:
From the Synchronized File I/O Integrity Documentation:
The SYNC and DSYNC options are used when opening a file to require that updates to the file are written synchronously to the underlying storage device. In the case of the default provider, and the file resides on a local storage device, and the seekable channel is connected to a file that was opened with one of these options, then an invocation of the write method is only guaranteed to return when all changes made to the file by that invocation have been written to the device. These options are 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. Whether this guarantee is possible with other provider implementations is provider specific.
Javadoc for SYNC and DSYNC options
In Linux/MacOS systems, this translates to the SYNC/DSYNC options for the open
function for opening files.
In Windows, either of those options being set translates to using the FILE_FLAG_WRITE_THROUGH
option, which can be seen in the source in WindowsChannelFactory:
if (flags.dsync || flags.sync)
dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
To use these flags, if you are unfamiliar with the nio
File API in Java it goes something like this:
Path file = Paths.get("myfile.dat");
SeekableByteChannel c = Files.newByteChannel(file, StandardOpenOption.SYNC);
you can use the channel directly to read/write data using byte buffers, or convert to a familiar input stream or output stream using the Channels class:
InputStream is = Channels.newInputStream(c);