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. 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
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()
So, when should I use NIO for file reading and writing? Why is it faster than traditional IO? What is the correct way to use it? Should I use IO or NIO only when reading and writing files?