我正在阅读《Java编程思想》这本书,其中讲解了
我已经查阅了以下信息:
java.nio.*
包,并表示NIO比传统的IO流读写文件更快。为什么呢?我已经查阅了以下信息:
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
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.
I think the need to use NIO is generally other advantages that need to use NIO, such as:
transferTo()/transferFrom()
那么,何时应该使用NIO进行文件读写?为什么它比传统IO更快?正确的使用方式是什么?在读写文件时,我应该只使用IO还是NIO?