Java NIO Tutorials

Java NIO Tutorials

In this detailed Resource page, we feature an abundance of Java NIO Tutorials!

The java.nio (NIO stands for Non-blocking I/O) is a collection of Java programming language APIs that offer features for intensive I/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I/O. NIO was developed under the Java Community Process as JSR 51. An extension to NIO that offers a new file system API, called NIO.2, was released with Java SE 7 (“Dolphin”).

Features and organization

The APIs of NIO were designed to provide access to the low-level I/O operations of modern operating systems. Although the APIs are themselves relatively high-level, the intent is to facilitate an implementation that can directly use the most efficient operations of the underlying platform.

The Java NIO APIs are provided in the java.nio package and its subpackages. The documentation by Oracle identifies these features.

  • Buffers for data of primitive types
  • Character set encoders and decoders
  • A pattern-matching facility based on Perl-style regular expressions (in package java.util.regex)
  • Channels, a new primitive I/O abstraction
  • A file interface that supports locks and memory mapping of files up to Integer.MAX_VALUE bytes (2 GB)
  • A multiplexed, non-blocking I/O facility for writing scalable servers

NIO buffers

NIO data transfer is based on buffers (java.nio.Buffer and related classes). These classes represent a contiguous extent of memory, together with a small number of data transfer operations. Although theoretically these are general-purpose data structures, the implementation may select memory for alignment or paging characteristics, which are not otherwise accessible in Java. Typically, this would be used to allow the buffer contents to occupy the same physical memory used by the underlying operating system for its native I/O operations, thus allowing the most direct transfer mechanism, and eliminating the need for any additional copying. In most operating systems, provided the particular area of memory has the right properties, transfer can take place without using the CPU at all. The NIO buffer is intentionally limited in features in order to support these goals.

There are buffer classes for all of Java’s primitive types except boolean, which can share memory with byte buffers and allow arbitrary interpretation of the underlying bytes.

Usage

NIO buffers maintain several pointers that dictate the function of their accessor methods. The NIO buffer implementation contains a rich set of methods for modifying these pointers:

  • The flip() method, rather than performing a “flip” or paging function in the canonical sense, moves the position pointer to the origin of the underlying array (if any) and the limit pointer to the former position of the position pointer.
  • Three get() methods are supplied for transferring data out of a NIO buffer. The bulk implementation, rather than performing a “get” in the traditional sense, “puts” the data into a specified array. The “offset” argument supplied to this method refers not to the offset from within the buffer from which to read, nor an offset from the position pointer, but rather the offset from 0 within the target array.
  • Unless using the absolute get() and put() methods, any get() or put() is conducted from the position pointer. Should one need to read from a different position within the underlying array, whilst not adjusting the writing position, the mark() and reset() methods have been supplied.
    The mark() method effectively stores the position of the position pointer by setting the mark pointer to the position of the position pointer. The reset() method causes the position pointer to move to the mark pointer’s position.
    Upon invocation of the clear() method or the flip() method the mark pointer is discarded.
  • The clear() method does not ensure zero-ing of the buffer, but does return the limit pointer to the upper boundary of the underlying array, and the position pointer to zero.put() and get() operations for NIO buffers are not thread safe.
  • You can only map() a java.nio.MappedByteBuffer from a java.nio.channels.FileChannel up to Integer.MAX_VALUE in size (2GiB); regions beyond this limit can be accessed using an offset greater than zero.

Channels

Channels (classes implementing the interface java.nio.channels.Channel) are designed to provide for bulk data transfers to and from NIO buffers. This is a low-level data transfer mechanism that exists in parallel with the classes of the higher-level I/O library (packages java.io and java.net). A channel implementation can be obtained from a high-level data transfer class such as java.io.File, java.net.ServerSocket, or java.net.Socket, and vice versa. Channels are analogous to “file descriptors” found in Unix-like operating systems.

File channels (java.nio.channels.FileChannel) can use arbitrary buffers but can also establish a buffer directly mapped to file contents using memory-mapped file. They can also interact with file system locks. Similarly, socket channels (java.nio.channels.SocketChannel and java.nio.channels.ServerSocketChannel) allow for data transfer between sockets and NIO buffers.

Selectors

A selector (java.nio.channels.Selector and sub-classes) provides a mechanism for waiting on channels and recognizing when one or more become available for data transfer. When a number of channels are registered with the selector, it enables blocking of the program flow until at least one channel is ready for use, or until an interruption condition occurs.

Although this multiplexing behavior could be implemented with threads, the selector can provide a significantly more efficient implementation using lower-level operating system constructs. A POSIX-compliant operating system, for example, would have direct representations of these concepts, select(). A notable application of this design would be the common paradigm in server software which involves simultaneously waiting for responses on a number of sessions.

Character sets

In Java, a character set is a mapping between Unicode characters (or a subset of them) and bytes. The java.nio.charset package of NIO provides facilities for identifying character sets and providing encoding and decoding algorithms for new mappings.

Reception

Unexpected is that a Channel associated to a Java IO RandomAccess file closes the file descriptor on an interrupt. While RandomAccessFiles own read method does not do this.

Note
If you wish to build up your Java NIO knowledge first, check out our Java Nio Tutorial for Beginners.

Java NIO Tutorials – Getting Started

Simple examples based on Java NIO

  • Java NIO Tutorial
    Java NIO is a library introduced from Java 1.4. Java NIO has since its launch provided an alternative way to handle I/O and networking transactions. It is considered to be an alternative to Java Networking and Java IO libraries.
  • Java Nio Echo Server Tutorial
    This article is a tutorial on implementing a simple Java NIO “echo server”. This example will take the form of a rather simple client server application whereby a client or many clients will connect to a running server and post message(s) to the server which will in turn be “echoed” back to the respective clients.
  • Java Nio Async HTTP Client Example
    This article is an example of how to build a simple asynchronous Http client using Java Nio. This example will make use of the httpbin service for much of it’s test cases, which can also be verified via postman or curl. Although the examples work, this is by no means a production ready.
  • Java Nio Create Directory Example
    Java NIO was developed to allow the Java programmers implement the high-speed input-output operations without using the custom native code. NIO moves the time-taking I/O activities like filling, namely and draining buffers etc back into the operating system, thus allows the great increase in the operational speed.
  • Java Nio FTP Example
    In this example we will demonstrate an FTP example program written in Java using some of the NIO features available to us. We will configure our local operating system to serve a specific directory via FTP. In order to simulate the FTP server we will make use of the vsftpd ubuntu package.
  • Java Nio Heartbeat Example
    This article is a tutorial on implementing a simple Java NIO Heartbeat. This example will take the form of “n” number of “Broadcast” mode processes which will multicast data via UDP to “n” number of “Subscribe” processes that have expressed interest in receiving said traffic.
  • Java Nio SSL Example
    This is an example of a non-blocking I/O provided by java.nio using SSL handshake. SSL is the secure communication protocol of choice for a large part of the Internet community. There are many applications of SSL in existence, since it is capable of securing any transmission over TCP.

Java NIO Tutorials – Functions

Learn the most famous functionalities and operations of the Java NIO

File

  • Java Nio Large File Transfer Tutorial
    This article is a tutorial on transferring a large file using Java Nio. It will take shape via two examples demonstrating a simple local file transfer from one location on hard disk to another and then via sockets from one remote location to another remote location.
  • Java Nio Iterate Over Files in Directory
    This example will demonstrate iterating over the files and directories recursively from a root directory. The example program will utilize some of the Java NIO features. Central to this example are the DirectoryStream, Filter, Path and Paths classes.
  • Java Nio Append File Example
    Java NIO (i.e. new I/O) is an interesting file input-output mechanism introduced in Java 5 and provides the different way of working with the input-output operations than the standard input-output API’s.
  • Java Nio Delete File Example
    If developers are working on a Java Swing or a desktop application then it may be required that sometimes developers need to delete a file from the file system. This tutorial is to learn about handling the files using the Java Nio package and shows how to delete a file in Java using the Nio package.
  • Nio Write File Example
    With this example we are going to demonstrate how to use the Non-blocking I/O API, or NIO.2 API (NIO API) for short, to write data to a file. The examples in this article are compiled and run in a Mac OS unix environment.
  • java.nio.file.Path Example
    This article introduces the Path interface and its basic usage. The Path interface is available in the Java SE 7 as part of Java NIO 2 File API. This article shows creating, getting information, converting and comparing paths. The examples in this article are compiled and run in Windows OS environment.
  • java.nio.file.WatchEvent Example
    WatchEvent<T> is an interface defined in the java.nio.file package. The type parameter T is the type of the context object associated with the event. This interface has been in Java since Java SE 7 as part of NIO 2 File APIs. This is part of file change notification API, called the Watch Service API. Watch event in general represents an event or a repeated event for an object that is registered with a WatchService.
  • 4 Ways to Copy File in Java
    Although Java offers a class that can handle file operations, that is java.io.File, it doesn’t have a copy method that will copy a file to another. The copying action is an important one, when your program has to handle many file related activities. Nevertheless, there are several ways you can perform a file copying operation in Java and we will discuss four of the most popular in this example.
  • Create memory mapped file
    This is an example of how to create a memory mapped file in Java.
  • Create shared file lock on file
    In this example we shall show you how to create a shared file lock in Java.
  • Create file lock on file
    This is an example of how to create a file lock in Java.

Channels

  • Java Nio Channels Example
    Channels are the second major innovation of the Java Nio after buffers. In Java Nio, channels are used for the input-output transfers and this tutorial explains how the Java Nio Channels are used to open the network connections and connections to the files.
  • java.nio.channels.FileLock Example
    This article introduces the FileLock class and its basic usage. This article examples show using file locks with FileChannels. The examples in this article are compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.
  • Java Nio Asynchronous Channels Tutorial
    The Asynchronous Channels API’s supplemented the core Java NIO API’s with additional functionality in the Java 7 release. Coined NIO.2 the supplement provided many utilities for NIO usage but the crown jewel was the AsynchronousChannel API’s.
  • Java Nio SocketChannel Example
    SocketChannel is a selectable channel belonging to the java.nio.channels package and is used for reading or writing the stream-oriented data. In this tutorial, we learn how to use the SocketChannel and how it is used for reading or writing the stream-oriented data by using the TCP based protocol.
  • Java Nio FileChannel Example
    The FileChannel is a Java Nio Channel implementation for working with a file. It facilitates reading, writing, mapping and manipulating a file. The examples in this tutorial will be demonstrated via test cases with no explicit Charset specified when encoding and decoding text from ByteBuffers.
  • Copying binary file with FileChannel
    With this example we demonstrate how to copy files using FileChannels in Java. In particular we are going to read data from a specific file in the file system and write them to another file.
  • Create Stream from FileChannel
    This is an example of how to create input and output streams to read and write data from/to a file in Java.
  • java.nio.channels.spi.SelectorProvider Example
    SelectorProvider is an abstract class defined in the java.nio.channels.spi package. This is a central service-provider class for selectors and selectable channels defined in the java.nio.channels API. A selector provider is a concrete subclass of this class that has a zero-argument constructor and implements the abstract factory methods of this class that return open channels and selector objects.
  • java.nio.channels.Selector Example
    This example shows the basic usage of Selector. This is an abstract class defined in the java.nio.channels package. Selector is a multiplexor of SelectableChannel objects.
  • java.nio.channels.ScatteringByteChannel Example
    ScatteringByteChannel is an interface extends ReadableByteChannel and is defined in java.nio.channels package. This is a channel that can read bytes into a sequence of buffers.
  • java.nio.channels.AsynchronousChannelGroup Example
    This article introduces the AsynchronousChannelGroup and its basic usage. This class is available since Java SE 7 as part of Java NIO 2 file API. This article’s example shows using this class with asynchronous channels. The example in this article is compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.
  • java.nio.channels.AsynchronousSocketChannel Example
    This article introduces the AsynchronousSocketChannel and its basic usage. This class is available since Java SE 7 as part of Java NIO 2 file API. This article’s example shows the socket channel client sending messages to an AsynchronousServerSocketChannel server – in a client/server setup. The example in this article is compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.
  • java.nio.channels.CompletionHandler Example
    This article introduces the CompletionHandler and its basic usage. This interface is available since Java SE 7 as part of Java NIO 2 File API. This article’s example shows reading from a file using asynchronous file channel and using the completion handler to consume its result. The example in this article is compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.
  • java.nio.channels.FileChannel Example
    This article introduces the FileChannel class and its basic usage. This class is available since Java 1.4 as part of Java NIO (New IO) File API. This article shows reading from and writing to file using file channels. The examples in this article are compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.
  • java.nio.channels.AsynchronousFileChannel Example
    This article introduces the AsynchronousFileChannel class and its basic usage. This class is available since the Java SE 7 as part of Java NIO 2 File API. This article shows reading from and writing to file using asynchronous file channels.
  • Java Nio Scatter/Gather Example
    In Java Nio, the channel provides an important capability known as scatter/gather or vectored I/O in some circles. Scatter/gather is a simple yet powerful concept and this tutorial explains how scatter/gather can be really useful in situations where developers need to separate work with the various parts of the transmitted data.
  • Java Nio Download File From Url Example
    Java NIO supports a buffer-oriented, channel-based approach for the I/O operations and with the introduction of Java 7, the NIO system has expanded thereby providing the enhanced support for the file system features and the file handling mechanism. In this tutorial, we will see how to download a file from the URL in Java.
  • Java Nio Socket Example
    This article introduces the SocketChannel class and its basic usage. This class is defined in the java.nio package. Socket programming involves two systems communicating with one another. In implementations prior to NIO, Java TCP client socket code is handled by the java.net.Socket class.

Buffer

  • Java Nio BufferOverflowException Example
    Exceptions are the unwanted or the unexpected events that occur during the execution of programs that disrupt the normal flow of the instructions. In this tutorial, we will learn about the BufferOverflowException which is very common in the Java Nio package. But before moving ahead let’s take a look and understand the basics of the Java Nio package.
  • Java Nio HeapByteBuffer Example
    This example demonstrates the usage of the Java Nio HeapByteBuffer. The Java Nio HeapByteBuffer is an odd class, one you will never reference directly and for good reason, it’s package private. Although it’s use is almost guaranteed when working with ByteBuffers unless you opt for a DirectByteBuffer (off heap).
  • Java Nio ByteBuffer Example
    This article is a tutorial on demonstrating the usage of the Java Nio ByteBuffer. All examples are done in the form of unit tests to easily prove the expectations of the API. The ByteBuffer class is an abstract class which also happens to extend Buffer and implement Comparable.
  • java.nio.Buffer Example
    This article introduces the Buffer class and its basic usage. This class is defined in the java.nio package. A buffer is a container for a fixed amount of data of a specific primitive type. There is one subclass of this class for each primitive type, except boolean. They are ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer and ShortBuffer classes. These are also defined in the java.nio package.
  • Java Direct ByteBuffer Example
    This example shows the usage of direct ByteBuffer. ByteBuffer is an abstract class, extends Buffer and implements Comparable<ByteBuffer>. This class is defined in the java.nio package. A buffer is a container for a fixed amount of data of a specific primitive type. There is a buffer class for each non-boolean primitive type. A ByteBuffer is a sub class of Buffer of byte primitive type.
  • Java MappedByteBuffer Example
    In this post, we are going to discuss about the class java.nio.MappedByteBuffer. There are two ways for reading a file, sequentially and randomly. Files that can be explored sequentially are known as sequential files. Files that permit random access to their contents are known as random access files (RAFs).
  • Convert Between Character Set Encodings with CharBuffer
    In this example we shall show you how to convert between character set encodings with a CharBuffer in Java.
  • Write to Channel with ByteBuffer
    With this example we are going to demonstrate how to write data to a NIO Channel using a ByteBuffer in Java. In particular we are going to read data from a specific file in the file system and write them to a destination file.
  • Read from Channel with ByteBuffer
    This is an example of how to read data from a NIO Channel using a ByteBuffer in Java. In particular we are going to read data from a specific file in the file system and print them on screen.
  • Save changes to memory mapped ByteBuffer from FileChannel
    In this example we shall show you how to write data to a file using a memory mapped ByteBuffer and a NIO FileChannel in Java.
  • Write/Append to File with ByteBuffer
    With this is example we are going to demonstrate how to write/append data to a file in Java using a ByteBuffer. Particularly we are going to read data from a source file and append them to the destination file.
  • Use ByteBuffer for non-byte Java types buffering
    In this example we will demonstrate how to perform non-byte Java types buffering using a ByteBuffer in Java.
  • Use ByteBuffer to store Strings
    This is an example of how to store Strings using a ByteBuffer in Java.
  • Get byte from ByteBuffer
    With this example we are going to demonstrate how to read bytes from a ByteBuffer. Additionally we will show you several of ByteBuffer‘s API methods in order to share some light on how to randomly ready data from it.
  • Put byte into ByteBuffer
    This is an example of how to put bytes into a ByteBuffer in Java. Additionally we will demonstrate several of ByteBuffer‘s API methods in order to share some light on how to randomly write data to it.
  • Create direct and non-direct ByteBuffer
    In this example we are going to demonstrate several methods of creating a direct (memory-mapped) and non-direct ByteBuffer in Java.
  • Convert between ByteBuffer and byte array
    With this example we are going to demonstrate how to convert between ByteBuffers and byte arrays.
  • Convert String to byte array UTF encoding
    With this example we are going to demonstrate how to convert a String to byte array and vice-versa using the default character encoding in Java. In short, to perform the aforementioned conversion, we are going to use classes from the NIO package in Java so as to convert every character from the target String to its byte equivalent.

[undereg]

Back to top button