Java IO vs. NIO17 Mar 2025 | 3 min read Let's see the table showing the main differences between Java IO and NIO:
Blocking vs. Non-blocking I/OBlocking I/O Blocking IO wait for the data to be write or read before returning. Java IO's various streams are blocking. It means when the thread invoke a write() or read(), then the thread is blocked until there is some data available for read, or the data is fully written. Non blocking I/O Non blocking IO does not wait for the data to be read or write before returning. Java NIO non- blocking mode allows the thread to request writing data to a channel, but not wait for it to be fully written. The thread is allowed to go on and do something else in a mean time. Stream Oriented vs. Buffer OrientedStream Oriented Java IO is stream oriented I/O means we need to read one or more bytes at a time from a stream. It uses streams for transferring the data between a data source/sink and a java program. The I/O operation using this approach is slow. Let's see the flow of data using an input/output stream in a java program: Buffer Oriented Java NIO is buffer oriented I/O approach. Data is read into a buffer from which it is further processed using a channel. In NIO we deal with the channel and buffer for I/O operation. The major difference between a channel and a stream is:
Therefore with the introduction of channel in java NIO, the non-blocking I/O operation can be performed. Let's see the interaction between channel, buffers, java program, data source and data sink: ![]() ChannelsIn Java NIO, the channel is a medium that transports the data efficiently between the entity and byte buffers. It reads the data from an entity and places it inside buffer blocks for consumption. Channels act as gateway provided by java NIO to access the I/O mechanism. Usually channels have one-to-one relationship with operating system file descriptor for providing the platform independence operational feature. NIO Channel Basics Channel implementation uses the native code to perform actual work. The channel interface allows us to gain access to low-level I/O services in a portable and controlled way. At the top of hierarchy, the Channel interface is used as given below: As we can see in above channel interface, the two operations common in all the channels are:
SelectorsIn Java NIO the selector is a multiplexor of selectable channels, which is used as a special type of channel that can be put into non-blocking mode. It can examine one or more NIO Channel's and determines which channel is ready for communication i.e. reading or writing. What is the use of Selector The selector is used for handling the multiple channels using a single thread. Therefore it require less threads to handle the channels. Switching between the threads is expensive for operating system. Therefore, for improving the system efficiency selector is use. Let's see the illustration of a thread using Selector to handle 3 Channel's: ![]() Creating a Selector We can create a selector by calling Selector.open() method, as given below: Next TopicJava NIO Channels |
Java In Java NIO the selector is a multiplexor of selectable channels, which is used as a special type of channel that can be put into non-blocking mode. It can examine one or more NIO Channel's and determines which channel is ready for communication i.e....
7 min read
Java The Java is used for connecting a channel with a TCP (Transmission Control Protocol) network socket. It is equivalent to Java Networking Sockets used in network programming. There are two methods available for creating a SocketChannel in Java NIO: It can be created when an incoming...
2 min read
Java The Java is also used for connecting a channel with a TCP (Transmission Control Protocol) network socket. It is equivalent to Java Networking Sockets used in network programming. The class of ServerSocketChannel is located in the java.nio.channels package. Let's see the example of ServerSocketChannel: ServerSocketChannel ssc...
2 min read
Java NIO Charset The concept of charset is introduced in JDK 1.4 using the package java.nio.charset.Charset. It plays an important role in encoding and decoding between the given charset and UNICODE. The name of charset must follow certain rules. It must begin with the number or letter....
1 min read
Java A Java is used for establishing the one-way data connection between two threads. It has a sink channel and source channel. The data is writing to the sink channel and this data can then be read from a source channel. In Java NIO the...
3 min read
Java In Java NIO, the channel is a medium used to transports the data efficiently between the entity and byte buffers. It reads the data from an entity and places it inside buffer blocks for consumption. Channels act as gateway provided by java NIO to access...
3 min read
Scatter/Gather or Vectored I/O In Java NIO the channel provides an important capability known as scatter/gather or vectored I/O. It is a simple yet powerful technique through which the bytes can be written from a set of buffers to a stream using a single write() function...
4 min read
Tutorial Java has provided a second I/O system called NIO (New I/O). provides the different way of working with I/O than the standard I/O API's. It is an alternate I/O API for Java (from Java 1.4). It supports a buffer-oriented, channel based approach for I/O operations....
2 min read
Java Buffers are defined inside java.nio package. It defines the core functionality which is common to all buffers: limit, capacity and current position. Java NIO buffers are used for interacting with NIO channels. It is the block of memory into which we can write data, which...
3 min read
Data Transfer between Channels In Java NIO we can directly transfer the data from one channel to another very frequently. The transfer of file data in bulk is very common that a couple of optimization methods have been added to FileChannel class for making it more...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India