[英]When Use FileChannel to read()/write() files?
I am reading the book Thinking in Java, which explains the java.nio.*
package and says that NIO is faster than reading and writing files with traditional IO streams.我正在阅读Thinking in Java这本书,它解释了java.nio.*
包并说 NIO 比使用传统 IO 流读取和写入文件更快。 Why?为什么?
I have reviewed the following information:我查看了以下信息:
IO stream is byte oriented, traditional IO processing unit is byte, and NIO processing unit is block (byte array), but I think traditional IO can also directly process block (byte array) through BufferedFile*, and traditional IO also has direct Method of processing byte array IO流是面向字节的,传统IO处理单元是字节,而NIO处理单元是块(字节数组),但是我觉得传统IO也可以通过BufferedFile*直接处理块(字节数组),而传统IO也有直接的方法处理字节数组
private native int readBytes(byte b[], int off, int len) throws IOException;
IO is blocking read, NIO can be non-blocking, but I found that the file NIO can only be non-blocking, then NIO has no advantage. IO是阻塞读,NIO可以是非阻塞的,但是我发现文件NIO只能是非阻塞的,那么NIO就没有优势了。
I think the need to use NIO is generally other advantages that need to use NIO, such as:我认为需要使用NIO的一般是其他需要使用NIO的优势,比如:
transferTo()/transferFrom()
So, when should I use NIO for file reading and writing?那么,什么时候应该使用 NIO 进行文件读写呢? Why is it faster than traditional IO?为什么比传统IO快? What is the correct way to use it?使用它的正确方法是什么? Should I use IO or NIO only when reading and writing files?我应该只在读写文件时使用 IO 还是 NIO?
There are only two cases where a FileChannel
is faster than a FileInputStream
or FileOutputStream
.只有两种情况FileChannel
比FileInputStream
或FileOutputStream
快。
The first is when you can use an off-heap ("direct") ByteBuffer
to hold data, so that it isn't copied into the Java heap.第一种是您可以使用堆外(“直接”) ByteBuffer
来保存数据,这样它就不会被复制到 Java 堆中。 For example, if you were writing a web-server that delivered static files to a socket, it would be faster to use a FileInputStream
and a SocketChannel
rather than a FileInputStream
and a SocketOutputStream
.例如,如果您正在编写一个将静态文件传送到套接字的 Web 服务器,使用FileInputStream
和SocketChannel
而不是FileInputStream
和SocketOutputStream
。
These cases are, in my opinion, very few and far between.在我看来,这些案例很少,而且相去甚远。 Normally when you read (or write) a file in Java you will be doing something with the data.通常,当您使用 Java 读取(或写入)文件时,您将对数据进行处理。 In which case, you can't avoid copying the data onto the heap.在这种情况下,您无法避免将数据复制到堆上。
The other use for a FileChannel
is to create a MappedByteBuffer
for random access to the contents of a file. FileChannel
的另一个用途是创建一个MappedByteBuffer
以随机访问文件的内容。 This is significantly faster than using RandomAccessFile
because it replaces explicit calls to the OS kernel with memory accesses that leverage the OS's paging mechanism.这比使用RandomAccessFile
快得多,因为它用利用操作系统分页机制的内存访问替换了对操作系统内核的显式调用。
If you're just getting started with I/O in Java, I recommend sticking with the classes in java.io
unless and until you can explain why switching to java.nio
will give you improved performance.如果您刚刚开始使用 Java 中的 I/O,我建议您坚持使用java.io
的类,除非您能解释为什么切换到java.nio
会提高性能。 It's much easier to use a stream-oriented abstraction than a block-oriented one.使用面向流的抽象比使用面向块的抽象要容易得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.