Search this site

 Home  I/O  Buffering  Character streams  NIO intro  Buffers  Channels  Buffer performance

Search this site:
Threads Database Profiling Regular expressions Random numbers Compression Exceptions C Equivalents in Java

 What do you think of this article? Did it help you? Found a mistake? Feedback and suggestions here

Introduction to Java NIO

The NIO ("New I/O") package1 was introduced in Java 1.4 to get round certain limitations of the original Java I/O package. The main features of NIO are:

  • buffers: the java.io package had essentially focussed on stream-based I/O; it is often fiddly to "read a block of data and then process it as a block";
  • memory-mapped files: on a related point, the java.io package did not provide file mapping, a feature offered by modern operating systems in which part of a file is effectively "mapped" into memory and accessed as though it were memory— see the section on mapped ByteBuffers for more details;
  • readiness selection: when managing concurrent network connections (and indeed, concurrently open streams in general), the traditional I/O package focussed on the conventional way of managing those connections, in which each connection is handled by a separate thread; modern operating systems provide more efficient methods, including readiness selection, in which one thread can poll and handle a number of connections;
  • direct transfer of data within kernel memory space: pre-NIO, stream data had to be manipulated at the Java level so that, for example, transferring data from one stream to another had to involve spurious buffer copying from the OS into Java and then back again; NIO, via the new concept of channels, provides a method to conveniently transfer data between files/media in Java;
  • locking: the standard I/O package did not deal with file locking (or, more broadly, concurrent access to parts of a file) explicitly.

Unforunately, NIO still did not address certain issues, some of which will be resolved in Java 7. For example, Java's support for file attributes and shortcuts or symbolic links is currently poor. And there is currently no Java support for file system notifications, in which an application is notified by the OS of events such as file modifications.


1. You're possibly wondering how to pronounce NIO. All I can tell you is that I've heard Sun employees pronounce it both en-eye-oh and nigh-oh, and I've heard other colleagues pronounce it nee-oh. Pick the one you prefer.

comments powered by Disqus

Written by Neil Coffey. Copyright © Javamex UK 2012. All rights reserved.