Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
考虑到 Java 7 中的 FileChannel 实现了 SeekableByteChannel 接口。使用 RandomAccessFile 而不是 FileChannel 有什么用吗?提前致谢。
RadomAccessFile并且FileChannel是完全不同的东西。 RandomAccessFile早在 NIO 之前就被引入了 java(例如通道)。当您必须在文件的指定位置(非顺序)读取和写入时,对文件进行操作很有用。然而,读和写操作被阻塞,例如,如果你调用 read 方法,它会被阻塞,直到字节被读取。
RadomAccessFile
FileChannel
RandomAccessFile
通道属于所谓的非阻塞 IO,它允许读取(和写入)字节而不被阻塞。RandomAccessFile和之间的唯一关系FileChannel是您可以通过随机访问文件访问频道。