Introduction to Java NIO with Examples
Last Updated :
10 Jun, 2020
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, we will understand more about Java NIO.
Java NIO(New Input/Output) is high-performance networking and file handling API and structure which works as an alternative IO API for Java. It is introduced from JDK 4. Java NIO works as the second I/O system after standard Java IO with some added advanced features. It provides a different way of working with I/O than the standard IO. Like
Java.io package which contains all the classes required for Java input and output operations, the
java.nio package defines the buffer classes which are used throughout NIO APIs. We use
Java NIO for the following two main reasons:
- Non-blocking IO operation: Java NIO performs non-blocking IO operations. This means that it reads the data whichever is ready. For instance, a thread can ask a channel to read the data from a buffer and the thread can go for other work during that period and continue again from the previous point where it has left. In the meantime, the reading operation is complete which increases the overall efficiency.
- Buffer oriented approach: Java NIO's buffer oriented approach allows us to move forth and back in the buffer as we need. The data is read into a buffer and cached there. Whenever the data is required, it is further processed from the buffer.
The main working of the Java NIO package is based on some core components. They are:
- Buffers: Buffers are available in this package for the primitive data types. Java NIO is a buffer oriented package. It means that the data can be written/read to/from a buffer which further processed using a channel. Here, the buffers act as a container for the data as it holds the primitive data types and provides an overview of the other NIO packages. These buffers can be filled, drained, flipped, rewind, etc.
- Channels: Channels are the new primitive I/O abstraction. A channel is a bit like stream used for communicating with the outside world. From the channel, we can read the data into a buffer or write from a buffer. Java NIO performs the non-blocking IO operations and the channels are available for these IO operations. The connection to different entities is represented by various channels which are capable of performing non-blocking I/O operation. The channels work as a medium or a gateway. The following image illustrates the channel and buffer interaction:
- Selectors: Selectors are available for non-blocking I/O operations. A selector is an object which monitors multiple channels for the events. As Java NIO performs the non-blocking IO operations, selectors and the selection keys with selectable channels defines the multiplexed IO operations. So, in simple words, we can say that the selectors are used to select the channels which are ready for the I/O operation. The following image illustrates the selector handling the channels:
Java NIO provides a new I/O model based on channels, buffers and selectors. So, these modules are considered as the core of the API. The following table illustrates the list of Java.nio packages for an NIO system and why they are used:
Package |
Purpose |
java.nio package |
It provides the overview of the other NIO packages.Different types of buffers are encapsulated by this NIO system, which are used throughout the NIO API's. |
java.nio.channels package |
It supports channels and selectors, which represent connections to entities which are essentially open the I/O connections and selects the channel ready for I/O. |
java.nio.channels.spi package |
It supports the service provider classes for java.io.channel package. |
java.nio.file package |
It provides the support for files. |
java.nio.file.spi package |
It supports the service provider classes for java.io.file package. |
java.nio.file.attribute package |
It provides the support for file attributes. |
java.nio.charset package |
It defines character sets and providing encoding and decoding operations for new algorithms. |
java.nio.charset.spi package |
It supports the service provider classes for java.nio.charset package. |
Why Java.nio.File, when Java.io.File is already present? It is a very common question that why we will move to Java.nio.File when Java.io.File is already present. There were a few things which were missing in the old Java.io.File and that led to the usage of the new Java.nio.File. The following are some of the things which were missing in the old package and the reasons why the new package is used:
- The old module provides limited support for the symbolic links.
- The old module provides limited support for file attributes and performance issues.
- The old module does not work consistently across all the platforms.
- The old module is missing with the basic operations like file copy, move, etc.
Refer to
this article to understand the difference between Java-IO and Java-NIO
Similar Reads
Java Native Interface with Example In the Programming World, it is often seen in the comparison of Java vs C/C++. Although the comparison doesn't make any such sense, as both the languages have their Pros and Cons, but what if we can use multiple languages in a single Program?In this article, we will learn How to use JNI(Java Native
4 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Java 9 Features with Examples Java is a general-purpose, high-level programming language developed by Sun Microsystems. It is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. Java was meant to follow the "Write Once Run Anywhere" (WORA) principle, i.e., J
6 min read
Introduction of BlueJ BlueJ is a Windows-based platform for the Java Development Kit (JDK). It is a free Java environment started in 1999 by Michael Kolling and John Rosenberg at Monash University, Australia, as a successor to Blue. It requires installing JDK version 1.3 or higher before installing BlueJ. It can be freel
4 min read
Java - Divide the Classes into Packages with Examples Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains vari
6 min read
Different Types of Classes in Java with Examples A class is a user-defined blueprint or prototype from which objects are created. Â It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order: Â Modifiers: A class can be public or has default access
11 min read
7 Things You Didnât Know About Java Undoubtedly, Java has been the most famous and widely used programming language out there. Not 1, or 2 but, today it covers almost every sector in the market. The reason is its adaptive nature and platform independence. By 2024, Java has already entered its 29th Anniversary and thereâs no looking ba
8 min read
Non Blocking Server in Java NIO Java NIO(New Input/Output) is high-performance networking and file handling API and structure which works as an alternative IO API for Java. It is introduced from JDK 4 which works as the second I/O system after standard Java IO with some added advanced features. It provides enhanced support for fil
8 min read
java.nio.channels.Selector Class in Java A selector is a tool that helps you keep an eye on one or more NIO channels and figure out when they're ready to transfer data. In Java NIO, the Selector is a key player that looks at multiple Java NIO Channel instances and figures out which ones are good to go for actions like reading or writing. T
9 min read
AsynchronousFileChannel in Java NIO Java.nio package was introduced in the version java 1.4 edition. It allows us to deal with different channels concurrently because it supports concurrency and multi-threading. The asynchronous file channel API is responsible for the java NIO package and it is defined under the NIO channels package.
5 min read