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-blocking 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.
This chapter is mainly concerned with the stream-based model of the java.io package. A short introduction
to some of the capabilities of the java.nio package is given in "A Taste of New I/O" on page 565, but the
use of non-blocking I/O and the java.net network I/O are advanced topics, beyond the scope of this book.
20.1. Streams Overview
The package java.io has two major parts: character streams and byte streams. Characters are 16-bit
UTF-16 characters, whereas bytes are (as always) 8 bits. I/O is either text-based or data-based (binary).
Text-based I/O works with streams of human-readable characters, such as the source code for a program.
Data-based I/O works with streams of binary data, such as the bit pattern for an image. The character streams
are used for text-based I/O, while byte streams are used for data-based I/O. Streams that work with bytes
cannot properly carry characters, and some character-related issues are not meaningful with byte
streamsthough the byte streams can also be used for older text-based protocols that use 7- or 8-bit characters.
The byte streams are called input streams and output streams, and the character streams are called readers and
writers. For nearly every input stream there is a corresponding output stream, and for most input or output
streams there is a corresponding reader or writer character stream of similar functionality, and vice versa.
Because of these overlaps, this chapter describes the streams in fairly general terms. When we talk simply
about streams, we mean any of the streams. When we talk about input streams or output streams, we mean the
byte variety. The character streams are referred to as readers and writers. For example, when we talk about the
Buffered streams we mean the entire family of BufferedInputStream,
BufferedOutputStream, BufferedReader, and BufferedWriter. When we talk about
Buffered byte streams we mean both BufferedInputStream and BufferedOutputStream.
When we talk about Buffered character streams, we mean BufferedReader and BufferedWriter.
The classes and interfaces in java.io can be broadly split into five groups:
The general classes for building different types of byte and character streamsinput and output streams,
readers and writers, and classes for converting between themare covered in Section 20.2 through to
Section 20.4.
•
A range of classes that define various types of streamsfiltered streams, buffered streams, piped
streams, and some specific instances of those streams, such as a line number reader and a stream
tokenizerare discussed in Section 20.5.
•
The data stream classes and interfaces for reading and writing primitive values and strings are
discussed in Section 20.6.
•
Classes and interfaces for interacting with files in a system independent manner are discussed in
Section 20.7.
•
The classes and interfaces that form the object serialization mechanism, which transforms objects into
byte streams and allows objects to be reconstituted from the data read from a byte stream, are
discussed in Section 20.8.
•
Some of the output streams provide convenience methods for producing formatted output, using instances of
the java.util.Formatter class. You get formatted input by binding an input stream to a
java.util.Scanner object. Details of formatting and scanning are covered in Chapter 22.