Java Reference
In-Depth Information
Chapter 20. The I/O Package
From a programmer's point of view, the user is a peripheral that
types when you issue a
read
request.
Peter Williams
The Java platform includes a number of packages that are concerned with
the movement of data into and out of programs. These packages differ
in the kinds of abstractions they provide for dealing with I/O (input/out-
put).
The
java.io
package defines I/O in terms of
streams.
Streams are
ordered sequences of data that have a
source
(input streams) or
des-
tination
(output streams). The I/O classes isolate programmers from the
specific details of the underlying operating system, while enabling access
to system resources through files and other means. Most stream types
(such as those dealing with files) support the methods of some basic in-
terfaces and abstract classes, with few (if any) additions. The best way
to understand the I/O package is to start with the basic interfaces and
abstract classes.
The
java.nio
package and its subpackages define I/O in terms of
buffers
and
channels.
Buffers are data stores (similar to arrays) that can be read
from or written to. Channels represent connections to entities capable
of performing I/O operations, including buffers, files, and sockets. The
"n" in
nio
is commonly understood as meaning "new" (the
nio
package
predates the original stream-based
io
package), but it originally stood
for "non-blocking" because one of the key differences between channel-
based I/O and stream-based I/O is that channels allow for non-block-
ing I/O operations, as well as interruptible blocking operations. This is a
powerful capability that is critical in the design of high throughput server-
style applications.
The
java.net
package provides specific support for network I/O, based
around the use of sockets, with an underlying stream or channel-based
model.