Chapter 6. New I/O

One of the major new features of Java 1.4 is a new Input/Output (I/O) architecture, intended for servers and other applications that require high-performance I/O. Features of this New I/O API include:

  • Network channels (the New I/O version of sockets) can be placed in nonblocking mode.

  • Multiple nonblocking channels can be multiplexed with a Selector object. This means that it is no longer necessary to create a new thread to monitor each network connection.

  • Files can be memory mapped.

  • Files, and sections of files, can be locked to prevent concurrent read and write access.

  • The new Charset class and related classes give better control over character-to-byte encoding and byte-to-character decoding.

  • The java.util.regex package supports text matching with Perl 5-style regular expressions. Although regular expressions are not directly related to I/O, they were developed under the umbrella of the same Java Specification Request (JSR) and are often considered to be part of the New I/O API.

This chapter demonstrates all of these features. To understand the examples, an overview of the New I/O API is in order. Except for the java.util.regex package already mentioned, the bulk of the New I/O is in java.nio and its subpackages. java.nio defines the Buffer class and various concrete subclasses, such as ByteBuffer and CharBuffer for holding sequences of bytes, characters, and other primitive types. All actual I/O is done with byte buffers, but values of other primitive types can ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.