简体   繁体   English

Java FileChannel.force()和StandardOpenOption.Sync之间的区别是什么?

[英]What is the diff between Java FileChannel.force() and StandardOpenOption.Sync?

I'm not sure if the new Java 7 nio.file.StandardOpenOption is different from the old FileChannel.force() method. 我不确定新的Java 7 nio.file.StandardOpenOption是否与旧的FileChannel.force()方法不同。

Is there a way to do O_DIRECT also? 有没有办法做O_DIRECT?

it is not possible to do O_DIRECT IO using the JVM. 无法使用JVM进行O_DIRECT IO。 One of the reasons (I think) is, that the memory where the file contents go need to be aligned to some 512 bytes boundaries. 原因之一(我认为)是文件内容所在的内存需要与512字节边界对齐。 Memory allocated with ByteBuffers don't fulfill this property. ByteBuffers分配的内存不满足此属性。

Another problem which is similar to this 512 bytes alignment is that you can only perform IO operations that are multiples of 512 bytes. 与此512字节对齐类似的另一个问题是,您只能执行512字节倍数的IO操作。 So if you want to read a file that has 700 bytes, you'll have troubles. 因此,如果您想读取一个700字节的文件,将会遇到麻烦。

Here is a similar thread that presents these issues. 这是介绍这些问题的类似话题。 I described a way in my blog how to implement direct IO into the JVM . 在博客中描述了一种如何将直接IO实现到JVM中的方法 It also contains a hint how you could add a O_DIRECT option to the StandardOpenOption class (you have to add the constant to the file /src/solaris/native/sun/nio/fs/genUnixConstants.c in the JDK sources) 它还暗示了如何向StandardOpenOption类添加O_DIRECT选项(必须在JDK源代码/src/solaris/native/sun/nio/fs/genUnixConstants.c常量添加到文件/src/solaris/native/sun/nio/fs/genUnixConstants.c中)。

I think the difference between the two is that StandardOpenOption does it automatically while you have to call FileChannel.force() to send the data to the underlying storage device. 我认为两者之间的区别在于StandardOpenOption是自动执行的,而您必须调用FileChannel.force()将数据发送到基础存储设备。 I'm not sure about O_DIRECT. 我不确定O_DIRECT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 StandardOpenOption.SYNC与StandardOpenOption.DSYNC - StandardOpenOption.SYNC vs StandardOpenOption.DSYNC 是否需要FileChannel.force和FileDescriptor.sync? - Are FileChannel.force and FileDescriptor.sync both needed? 如果Filechannel.force(true)引发异常,则写入的数据将如何处理? - What happens with written data if Filechannel.force(true) throws an exception? RandomAccessFile.close()是否在内部调用FileChannel.force()? - Does RandomAccessFile.close() internally call FileChannel.force()? 使用StandardOpenOption.SYNC打开基础通道时,是否必须刷新MappedByteBuffer - Do we have to flush MappedByteBuffer when underlying channel is opened with StandardOpenOption.SYNC 为什么 StandardOpenOption.DELETE_ON_CLOSE 不删除 FileChannel 的源文件? - Why is StandardOpenOption.DELETE_ON_CLOSE not deleting the source file of the FileChannel? StandardOpenOption.SPARSE的用途是什么? - What is the use of StandardOpenOption.SPARSE? FileChannel#强制和缓冲 - FileChannel#force and buffering 有/没有 TRUNCATE_EXISTING 的 StandardOpenOption.WRITE + StandardOpenOption.CREATE 之间的区别? - Difference between StandardOpenOption.WRITE + StandardOpenOption.CREATE with/without TRUNCATE_EXISTING? java映射的FileChannel实现 - java mapped FileChannel implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM