Java 使用类似于 Windows c++ FILE_FLAG_WRITE_THROUGH 的选项打开文件
Java open file with option similar to the Windows c++ FILE_FLAG_WRITE_THROUGH
在 C++ 应用程序中打开(或创建)文件时,您可以向操作系统提供提示以满足您的需要。例如,如果您想自定义缓存管理器的行为,您可以禁用每个文件的写入缓存。
有没有办法在打开带有标志 FILE_FLAG_WRITE_THROUGH?
的文件时实现与 c++ 中可能的效果相同的效果
来自:https://msdn.microsoft.com/en-gb/library/windows/desktop/aa363858(v=vs.85).aspx#caching_behavior
A write-through request via FILE_FLAG_WRITE_THROUGH also causes NTFS to flush any metadata changes, such as a time stamp update or a rename operation, that result from processing the request.
我知道 JNI 可以做到这一点,但是 POSIX 中有一个类似的选项 (O_DIRECT
) 我想知道 java 是否以某种方式支持这种功能.
有StandardOpenOption.SYNC
和StandardOpenOption.DSYNC
:
来自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
在 Linux/MacOS 系统中,这转换为 opening files 的 open
函数的 SYNC/DSYNC 选项。
在 Windows 中,设置的这些选项中的任何一个都转换为使用 FILE_FLAG_WRITE_THROUGH
选项,可以在来源 in WindowsChannelFactory:
中看到
if (flags.dsync || flags.sync)
dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
要使用这些标志,如果您不熟悉 Java 中的 nio
文件 API,它是这样的:
Path file = Paths.get("myfile.dat");
SeekableByteChannel c = Files.newByteChannel(file, StandardOpenOption.SYNC);
您可以使用字节缓冲区直接将通道用于 read/write 数据,或者使用 Channels class:
转换为熟悉的输入流或输出流
InputStream is = Channels.newInputStream(c);
在 C++ 应用程序中打开(或创建)文件时,您可以向操作系统提供提示以满足您的需要。例如,如果您想自定义缓存管理器的行为,您可以禁用每个文件的写入缓存。
有没有办法在打开带有标志 FILE_FLAG_WRITE_THROUGH?
的文件时实现与 c++ 中可能的效果相同的效果来自:https://msdn.microsoft.com/en-gb/library/windows/desktop/aa363858(v=vs.85).aspx#caching_behavior
A write-through request via FILE_FLAG_WRITE_THROUGH also causes NTFS to flush any metadata changes, such as a time stamp update or a rename operation, that result from processing the request.
我知道 JNI 可以做到这一点,但是 POSIX 中有一个类似的选项 (O_DIRECT
) 我想知道 java 是否以某种方式支持这种功能.
有StandardOpenOption.SYNC
和StandardOpenOption.DSYNC
:
来自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
在 Linux/MacOS 系统中,这转换为 opening files 的 open
函数的 SYNC/DSYNC 选项。
在 Windows 中,设置的这些选项中的任何一个都转换为使用 FILE_FLAG_WRITE_THROUGH
选项,可以在来源 in WindowsChannelFactory:
if (flags.dsync || flags.sync)
dwFlagsAndAttributes |= FILE_FLAG_WRITE_THROUGH;
要使用这些标志,如果您不熟悉 Java 中的 nio
文件 API,它是这样的:
Path file = Paths.get("myfile.dat");
SeekableByteChannel c = Files.newByteChannel(file, StandardOpenOption.SYNC);
您可以使用字节缓冲区直接将通道用于 read/write 数据,或者使用 Channels class:
转换为熟悉的输入流或输出流InputStream is = Channels.newInputStream(c);