2

两者中哪一个是最佳选择,在什么情况下?显然,将文件通道用于非常小的文件是没有意义的。除此之外,两种输入/输出方式的优缺点是什么。提前非常感谢。

4

1 回答 1

1

FileChannel 有许多 java.io 中缺少的特性:它是可中断的,它可以在文件中移动位置,它可以锁定文件等。它可以比旧 IO 更快,特别是当它使用直接字节缓冲区时,这里有一个ByteBuffer API 的解释:

byte buffer is either direct or non-direct. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations. 

如果您不需要上述任何功能与流一起使用,您将获得更短的代码。

于 2013-05-10T08:55:57.993 回答