Java NIO Tutorial

Java NIO Tutorial

The New Input/Output (NIO) was introduced with Java Development Kit (JDK 1.4 codename Merlin) back in February 2002. Its goal was to address many of the shortcomings of the original standard Java I/O classes. In this tutorial, we will discuss many aspects of the NIO Library.

Purpose of our Java NIO Tutorials

Below are some of the focus for our Java NIO tutorial series:

  • Easy way to get started with Java NIO
  • Using Asynchronous / Non-Blocking Java NIO
  • Implementing High-Performance I/O code using the Java NIO package
  • Easy way to get use and implement Java NIO

Purpose of NIO

The Java NIO Library offers high-performance features and functionality for handling I/O operations. This library supports that by interfacing directly with native channels, direct memory buffers and via enhanced socket support using selectors. By using NIO library programmers are able to implement high-speed I/O solutions without having to write native code (JNI). Additionally, it provides development of non-blocking I/O operations

Differences of NIO vs Traditional Java I/O

The traditional Java I/O API focuses on I/O Streams characterized by (Byte Streams, Character Streams,Buffered Streams,Data Streams and Object Streams). In stream-oriented system, data moves one byte at a time, through an object called a stream and is used to represent either an input source or an output source. Streams support many different kinds of data including simple bytes, primitive data types, character data types using unicode conventions and object types from serializable objects.

NIO by contrast, work with data in blocks via an object called a Buffer. Each of these operations produces or consumes the block of data in a single step. NIO improves performance by delegating the most time-consuming I/O operations (mainly filling and emptying buffers) back to the operating system, resulting in speed increase.

NIO Buffers

Buffers are a cornerstone upon which the NIO operations are built. Basically, all operations that involve NIO use buffers as a staging area for transfering data in and out from the datasource to/from target. In the NIO library, data travels into buffers and out of buffers on a regular basic. Anytime you write data, you are writing data into a buffer and when you read data you are reading from a buffer. Buffers interact with NIO Channels directly.

NIO Channels

Channels are akin to Streams in traditional Java I/O API with the exceptions that they can provide three modes: input, output or bi-directional. Streams on the other hand, where uni-directional (you were either using InputStream or OutputStream). Channels interact directly with buffers and the native IO source, that is, file, or socket.

NIO Selectors

A selector allows for multiplexing of NIO Channels. Selectors give the user the ability to work with multiple channels and provide channel readiness selection.

We will explain how all of this works in detail in this tutorial series.


Java NIO Related Tutorials

  • Java NIO Tutorial
    In this tutorial series we discuss the new features of Java NIO.
  • Java NIO Buffers
    This post covers NIO Buffers in more detail and provides practical examples of using buffers in real world applications.
  • Java NIO Channels
    This post covers NIO Channels in more detail and provides examples on network connections and Java I/O in relation to files.
  • Java NIO Selectors
    In this tutorial we learn how to use the Selector class from the package library to build High-Performance I/O client-server using NIO.
  • Java NIO File Channel
    In this tutorial we learn how to use the FileChannel class from the package library and provide working examples on all of the main methods.
  • Java NIO Socket Channel
    In this tutorial we learn how to use the SocketChannel and how it is used for reading/writing stream oriented data and using TCP connection based protocol.
  • Java NIO DatagramChannel Tutorial
    In this tutorial we learn how to use the DatagramChannel to allow developers to build high-performant data streaming applications that send and receive datagrams using a protocol called UDP.
  • Java NIO and NIO2 Path Tutorial
    This tutorial will introduce the Path interface and many of its methods. The Path interface was made available as part of the Java SE 7 release in the Java NIO 2 File API.

Please Share Us on Social Media


Leave a Reply

Your email address will not be published. Required fields are marked *