Java 谁的filechannel或RandomAccessFile读写性能更好?

Java 谁的filechannel或RandomAccessFile读写性能更好?,java,nio,randomaccessfile,filechannel,Java,Nio,Randomaccessfile,Filechannel,我最近遇到了,我是一个超级粉丝。但是我想知道为什么我会选择FileChannel而不是RandomAccessFile来读取一个文件并将该内容写入另一个文件 是否有任何具体的性能原因?我不想出于任何目的使用FileChannel锁定,因为我相信这可能是可以使用FileChannel的原因之一。我不想像StackOverflow响应中建议的那样使用BufferReader或类似的东西 FileChannel API说:文件的一个区域可以直接映射到内存中;对于大型文件,这通常比调用通常的读或写方法更

我最近遇到了,我是一个超级粉丝。但是我想知道为什么我会选择
FileChannel
而不是
RandomAccessFile
来读取一个文件并将该内容写入另一个文件


是否有任何具体的性能原因?我不想出于任何目的使用
FileChannel
锁定,因为我相信这可能是可以使用FileChannel的原因之一。我不想像StackOverflow响应中建议的那样使用
BufferReader
或类似的东西

FileChannel API说:文件的一个区域可以直接映射到内存中;对于大型文件,这通常比调用通常的读或写方法更有效

RandomAccessFile性能良好,并且允许直接读取和写入大多数主要类型。

文件通道可供多个并发线程安全使用。

除非您使用带有直接缓冲区的
文件通道
,并且自己从不访问数据,例如,您只需将其复制到
SocketChannel。
这会更快,因为数据不必跨越JNI/JVM边界


但我想知道你为什么不选择
BufferedReader
。逐行读取文件肯定比这两种方法快几个数量级

随机访问文件源:

请看RandomAccessFile实际上是在后台使用FileChannel


为什么?好,目前我正在使用
transferTO
方法来读取和复制
FileChannel
是一个很好的理由。我的意思是我没有使用它,但最终我想但不确定如何确定这是否比RAFB更好,但他们提到了FileChannel的读写本身,而不是任何其他读写方法。这是不对。FileChannel不在RandomAccessFile内部使用,仅在调用getChannel时进行初始化。本机方法用于打开/读取/写入基础文件。它作为公共方法提供,供任何人获取对基础文件的FileChannel的引用。
public final FileChannel getChannel() {
         synchronized (this) {
             if (channel == null) {
                 channel = FileChannelImpl.open(fd, true, rw, this);

                 /*
                  * FileDescriptor could be shared by FileInputStream or
                  * FileOutputStream.
                  * Ensure that FD is GC'ed only when all the streams/channels
                  * are done using it.
                  * Increment fd's use count. Invoking the channel's close()
                  * method will result in decrementing the use count set for
                  * the channel.
                  */
                 fd.incrementAndGetUseCount();
             }
             return channel;
         }
     }