Java Reference
In-Depth Information
Input-Output (I/O)
Packages
java.io
Use:
J2SE (since version 1.0)
Description
The main goal of the Java I/O API is to allow developers to use streams. Streams provide basic input-output
capabilities in Java. If you want to write to a file, use a stream; if you want to read from standard input, use a
stream. If you want to write across a network—well, you get the idea.
The
java.io
package contains four general types of stream, which are based on four abstract classes. These
classes provide functionality based on stream direction (input or output) and level whether the stream information
is based on bytes or characters.
Table 6-3. Stream types in the
java.io
package
Low-level (bytes)
High-level (character
s)
Input
Output
I
nput
Output
InputStream
OutputStream
Reader
Writer
All other streams in Java subclass one of these four classes, and extend the functionality of the class by adding a
specific capability. For example, the
FileWriter
is a type of
Writer
(output, writes characters) and it adds the
ability to write characters to a file on disk. The
DataInputStream
is a kind of
InputStream
(input, reads bytes)
and it also allows developers to read different data types, such as an
int
,
float
, or
boolean
value.
How do you create a stream with combinations of abilities in Java? You “chain” them together, using the concept
of a filter stream. You can attach filter streams to other streams by passing the target stream into the filter's
constructor. The filter can then add its own functionality to the associated stream. For example, you could read
lines of text from the standard input stream by using these lines of code:
Example 6.2
Streams in Java
BufferedReader readIn =
new BufferedReader(new InputStreamReader(System.in));
String textLine = readIn.readLine();
In this example, input ultimately comes from
System.in
(keyboard input) in the form of bytes. By adding an
InputStreamReader
, you can use the passed bytes to make Java language characters. Finally, the
BufferedReader
places the characters from the
InputStreamReader
into a buffer. The
BufferedReader
can
detect when the end of line is reached, and release the buffered characters as a
String
.
There aren't a lot of stream classes in the
java.io
package. However, since you can mix and match filter streams,
you ultimately have much more functionality available than you might think. You can think of Java I/O as a
pipeline. When you write code, you attach different I/O objects, or “pipes,” to each other. With each section of
pipe added, you modify the flow through the pipeline.
Pattern Use
It's evident that a lot of I/O programming involves stream chaining. To support this capability, the API relies
Each of the four abstract classes—
InputStream, OutputStream, Reader
and
Writer
—acts as the base for a
decorator chain. The I/O classes that support decorator behavior have one or more constructors that accept an
argument of another I/O class to chain.
java.io
contains the following categories of I/O classes:
Base I/O classes (also called node streams) - These classes provide endpoints of communication; they are
actually attached to some end location. For example, a
FileReader
is not a decorator because it is directly
connected to a file.