The essence and difference between IO and NIO in Java


The full name of IO is input output, is the bridge between the java program and the outside world, IO refers to all the classes in the package, they exist from java 1.0. NIO is called new IO, is the new generation of IO introduced in java 1.4.

What is the nature of IO? What is the difference between it and NIO? How do we learn IO and NIO?

Don’t worry, after reading this article everything has an answer.

For more content, please visit

The nature of IO

The role of IO is to read data from an external system into the java program or to write the data output from the java program back to the external system. Here the external system may be disk, network streams etc.

Because the processing of all external data is implemented by the operating system kernel, for the java application, it just calls the corresponding interface methods in the operating system, thus interacting with the external data.

The essence of all IO is the processing of Buffer, we put data into Buffer for the system to write external data, or read data from the system Buffer read from the external system.

The user space which is our own java program has a Buffer and the system space also has a buffer. so there will be a situation where the system space caches the data, in this case the system space will return the data in the Buffer directly to improve the read speed.

DMA and virtual address space

Before we continue, let’s explain two basic concepts in operating systems to facilitate our understanding of IO later.

Modern operating systems have a component called DMA (Direct memory access). What does this component do?

Without DMA, if a program performs an IO operation, all the CPU time is taken up, and the CPU cannot respond to other tasks, but has to wait for the IO to finish. This is unthinkable in modern applications.

If DMA is used, the CPU can pass the IO operation to other OS components, such as the data manager, and only when the data manager is finished, it will notify the CPU that the IO operation is complete. DMA is basically implemented in modern operating systems.

Virtual address space is also called (Virtual address space), in order to isolate different programs from each other and ensure the certainty of addresses in the program, modern computer systems have introduced the concept of virtual address space. In simple terms, it can be seen as a mapping to the actual physical address, by using segmentation or paging techniques to map the actual physical address to the virtual address space.

We can map both the system space buffer and the user space buffer to the same place in the virtual address space. This way the step of copying from system space to user space is omitted. It will be much faster.

Also to solve the problem of virtual space being larger than physical memory space, modern computer technology generally uses paging technology.

Paging is the process of dividing the virtual space into many pages and only assigning a map to the physical memory when it is needed, so that the physical memory can actually be seen as a cache of virtual space addresses.

The impact of virtual space address paging on IO is that IO operations are also based on page.

Some common page sizes are: 1,024, 2,048, and 4,096 bytes.

Classification of IO

IO can be divided into two categories: File/Block IO and Stream I/O.

For File/Block IO, the data is stored in the disk, which is managed by the filesystem. We can define the name, path, file attributes, etc. of the file through the filesystem.

The filesystem manages the data by dividing it into data blocks. Some blocks store the metadata of the file and some blocks store the real data.

Finally filesystem paginates the data as it processes it. filesystem pagination can be the same size as the memory pagination or a multiple of it, such as 2,048 or 8,192 bytes.

Not all data is in the form of blocks, we have another type of IO called stream IO.

Stream IO is like a pipeline stream in which the data is consumed in sequence.

Difference between # IO and NIO

IO in java 1.0 is stream IO, which can only process data byte by byte, so IO is also called Stream IO.

And NIO is born to improve the efficiency of IO, it reads data in Block.

In Stream IO, input is one byte, output is one byte, because it is Stream, you can add filter or filter chain, think about the filter chain in web framework. in Stream IO, data can only be processed once, you can’t back out data in Stream.

In Block IO, data is processed in the form of blocks, so its processing speed is faster than Stream IO, and you can fall back on processing data. But you need to handle the buffer yourself, so the complexity is higher than Stream IO.

Generally speaking, Stream IO is blocking IO, when a thread reads or writes, the thread will be blocked.

NIO is generally non-blocking, which means that you can do other operations during the read or write process, and the NIO operation will be notified of the completion of the read or write operation after it is executed.

In IO, it is mainly divided into DataOutPut and DataInput, which correspond to IO out and in respectively.

DataOutPut has three main classes, namely Writer, OutputStream and ObjectOutput.

Look at the inheritance relationship among them.



DataInput also has three main classes, ObjectInput, InputStream and Reader.

Look at their inheritance relationships.



ObjectOutput and ObjectInput classes are relatively few, so they are not listed here.

Statistics about 20 classes, figure out the use of these 20 classes, congratulations on java IO you understand!

For NIO is a little more complicated, first of all, in order to handle the information of the block, you need to read the data into the buffer, so in NIO Buffer is a very important concept, we look at the Buffer in NIO: !


From the above figure we can see that there are various buffer types prepared for us to use in NIO.

Another very important concept is the channel, which is the channel through which NIO gets its data: !


NIO needs to master the number of classes slightly more than IO, after all, NIO is a little more complex.


We will introduce more knowledge about io and nio later, so stay tuned.