Java IO(Input/Output) is used to perform read and write operations. The java.io package contains all the classes required for input and output operation. Whereas, Java NIO (New IO) was introduced from JDK 4 to implement high-speed IO operations. It is an alternative to the standard IO API’s. In this article, the difference between these two IO packages is discussed.
Before getting into the difference between java IO and Java NIO, we need to understand a few key concepts which differentiate both the I/O packages:
- Stream oriented vs Buffer Oriented Packages:
Java IO is a stream-oriented package which means that it can be read one or more bytes at a time from a stream. It uses a stream for transferring the data between data source/sink and java program. It is a unidirectional data transfer. The following image illustrates a stream-oriented package:
Unlike Java IO, Java NIO is a buffer oriented package. This means that the data is read into a buffer from which it is further processed using a channel. For example, a thread asking a channel to read data into a buffer and while the channel is reading data to the buffer simultaneously the thread can go for some other work. Once the data is read into the buffer, the thread can then continue to process the work which it had left during the read operation. Therefore, NIO is a bi-directional data transfer. The following image illustrates a buffer-oriented package:
- Blocking IO vs Non Blocking IO Packages:
Java IO is a blocking IO. This means that if a thread is invoking a read() or write() operation, that thread is blocked until there is some data to read or the data is fully written. That’s why it is synchronous IO or blocking IO.
Unlike Java IO, Java NIO is a non-blocking IO. This means that if a thread is invoking a read() or write() operation, that thread is not blocked until there is some data to read or the data is fully written rather the thread go on something else. That’s why it is an asynchronous IO or non-blocking IO.
A channel is a medium for efficient data transmission between entity and buffer. It acts as a gateway for open connection with data source/sink.
The selector selects the channel among the multiple IO channels using the single thread.
The following table illustrates the differences between the Java IO and Java NIO:
|Java IO||Java NIO|
|Java IO stands for Java Input Output||Java NIO stands for Java New Input Output|
|Java IO operates inside java.io package||Java NIO operates inside java.nio package|
|Java IO is Stream oriented||Java NIO is Buffer oriented|
|Blocking IO operation||Non-blocking IO operation|
|Channels are not available||Channels are available||It deals with data in stream||It deals with data in blocks|
|Does not contain the concept of Selectors||Contains the concept of Selectors|
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Introduction to Java NIO with Examples
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Similarities and Difference between Java and C++
- Difference between Core Java and Advanced Java
- Difference between a Java Application and a Java Applet
- Difference between Java and Core Java
- Difference between Stop and Wait protocol and Sliding Window protocol
- Difference between Yaacomo and and XAP
- Difference between VoIP and and POTS
- Difference and Similarities between PHP and C
- Difference between Time Tracking and Time and Attendance Software
- Difference Between Single and Double Quotes in Shell Script and Linux
- Difference between Abstract Class and Interface in Java
- Difference between == and .equals() method in Java
- Difference between x++ and x=x+1 in Java
- Difference between super() and this() in java
- Difference between Simple and Compound Assignment in Java
- Difference between throw and throws in Java
- Difference between notify() and notifyAll() in Java
- Difference between Traditional Collections and Concurrent Collections in java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.