Skip to main content

Java nio Package

The Java NIO (New I/O) package is a set of classes and interfaces that provides an alternative to the standard I/O API in Java. It was introduced in Java 1.4 and has been improved in subsequent releases. The package is designed to be more efficient, flexible, and scalable than the traditional I/O API. In this article, we will explain the main concepts and components of the Java NIO package.


Buffers are the fundamental building blocks of the NIO package. A buffer is an object that contains a fixed amount of data that can be read or written. Buffers are used to transfer data between channels and between channels and files. The NIO package provides four types of buffers: ByteBuffer, CharBuffer, ShortBuffer, and IntBuffer. The ByteBuffer is the most commonly used buffer and is used to transfer binary data.


Channels are similar to streams in the traditional I/O API but provide more functionality. A channel is a conduit for I/O operations and can read data from a source or write data to a destination. Channels can be used to read from and write to files, sockets, and other I/O sources. The NIO package provides several types of channels, including FileChannel, SocketChannel, DatagramChannel, and ServerSocketChannel.


Selectors are a key feature of the NIO package that enables multiplexed I/O operations. A selector allows a single thread to handle multiple channels by selecting the channels that are ready for I/O operations. This is more efficient than using multiple threads, as it reduces context switching and thread overhead. Selectors work by registering channels with the selector and then waiting for events on those channels. When an event occurs, the selector wakes up and provides a set of keys that represent the channels that have events.

Non-blocking I/O

Non-blocking I/O is a feature of the NIO package that enables asynchronous I/O operations. With non-blocking I/O, a channel can be set to non-blocking mode, which means that the channel will not block when it performs I/O operations. Instead, the channel will return immediately and indicate whether the operation was successful or not. This is useful when multiple channels need to be managed by a single thread, as it allows the thread to continue processing other channels while waiting for I/O operations to complete.

File I/O

The NIO package provides several classes and interfaces for performing file I/O operations. The most commonly used class is FileChannel, which provides methods for reading and writing data to a file. The package also provides classes for mapping files to memory, reading and writing file metadata, and creating and deleting files and directories.


The NIO package provides classes and interfaces for performing networking operations, such as connecting to servers, sending and receiving data over sockets, and setting up multicast connections. The most commonly used classes for networking are SocketChannel, DatagramChannel, and ServerSocketChannel.


The Java NIO package provides a flexible and efficient alternative to the standard I/O API in Java. It offers buffers for efficient data transfer, channels for performing I/O operations, selectors for multiplexed I/O, and non-blocking I/O for asynchronous operations. It also provides classes for performing file I/O and networking operations. The NIO package is especially useful for network and server programming, as it allows for efficient management of multiple channels and reduces thread overhead.

Where is Java nio package?

nio package are exist in java.nio package.

What is included in Java nio?

There is file directory of java.nio package.

  •  channels
  •  charset
  •  file


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment