Java NIO Package Tutorial

In this section we are presenting you Java NIO Package tutorials and examples.

Java NIO Package Tutorial

In this section we are presenting you Java NIO Package tutorials and examples.

Java NIO Package Tutorial

Java NIO Package Tutorial - Learn and master NIO Package with many examples

In this section we will learn about the java.nio package and see how to make responsive application using this package. We have also included many articles, tutorials and example of using the NIO package in Java programming. If you are beginner in Java then these tutorials make you perfect in Java NIO package. You should have prior experience in core Java basic concepts and you are able to make programs in Java to learn learn tutorials given in this section.

In this section will also see how to use java.nio package classes and its methods in Java programming for writing better applications. So, let's get started with the java.nio package.

Example presented here is tested on Java 11 and works well. The java.nio package was first introduced in Java 1.4 and since then there is lot of improvements is added to java.nio package. We are using Java 11 and tested example code with this version of JDK. You will find great use of java.nio package while developing application using the JDK 11. Java NIO package provides advanced and efficient I/O functionality to the Java programmers.

The java.nio (known as java new input output) package was introduced in Java 1.4 which is broadly used in data processing and it also provides better scalability. The benefits of using this is to read and write block of data rather byte by byte from the disk. The Java I/O API works with byte streams and character stream but NIO I/O API works with channel and buffers making it faster while performing I/O operations. A Java NIO FileChannel is used to connect to a file and this channel can be used for better I/O file operations.

Abstract classes in java.nio package

The java.nio package contains following abstract classes:

Class Details
Buffer Buffer is basically a container for data of a specific primitive type which also is a linear and finite sequence of elements. The other important properties of buffer are its capacity, limit and position which are used while reading and writing into the buffer.
ByteBuffer The ByteBuffer class defines methods for reading and writing values of all primitive type (char, short, int, long, and double) except the boolean type.
CharBuffer The Character Buffer is created either allocating space for buffer's content, by wrapping and existing char array into a buffer or by creating a view of an existing byte buffer.
DoubleBuffer The Double Buffer is created either allocating space for buffer's content, by wrapping and existing double array into a buffer or by creating a view of an existing byte buffer.
FloatBuffer The Float Buffer is created either allocating space for buffer's content, by wrapping and existing float array into a buffer or by creating a view of an existing byte buffer.
IntBuffer The int Buffer is created either allocating space for buffer's content, by wrapping and existing int array into a buffer or by creating a view of an existing byte buffer.
LongBuffer The long buffer is created either allocating space for buffer's content, by wrapping and existing long array into a buffer or by creating a view of an existing byte buffer.
MappedByteBuffer The Mapped byte buffers is created with the map method of FileChannel class available in  java.nio.channels package. The contents of a mapped byte buffer can be changed at any time.  If the mapped file is truncated then all or part of a mapped byte buffer may be inaccessible at any time.
ShortBuffer The short buffer is created either allocating space for buffer's content, by wrapping and existing shortarray into a buffer or by creating a view of an existing byte buffer.

Example of java.nio package

Now we will see examples on java.nio package and how it is implemented in java programming.

  1. How to use Java Path class?
  2. java.nio ShortBuffer Example