Java I/O seeks to provide a consistent framework
that works the same essential way regardless of the sort of I/O
involved. That framework centers around the concept of the stream,
which is a sequential flow of bytes in one direction. For example,
an output stream carries text to the console and an input stream
brings text from the keyboard. An output stream carries data to
a file and an input stream brings data from a file. An input stream
brings in data from a network socket. An output stream sends objects
to a distant computer on the network.
The bulk of Java I/O classes belong to the java.io
package. The class heirarchies build on the base classes InputStream,
provide a wide range of input and output tasks. Also, there are
stream classes that wrap a
stream to add more capabilties to it.
This diagram shows most of the members of the java.io package. Rectangles
parallelograms are abstract classes, and ovals are interfaces.
Some I/O classes provide a destination or source,
such as a file or an array. Others process the stream in some way
such as buffering or filtering the data. In this many of these classes
in this chapter.
Packages involving I/O include:
- the primary Java I/O classes (see above diagram.)
a new set of packages with Java 1.4 that uses the concept of channels
that represent an open connection to a hardware device, file,
or other entity. The classes provide various features such as
buffering and multiplexing among different channels. Channels
don't supplant streams but rather work with them to add additional
capabilities and enhanced scaling when working with large numbers
- I/O over the network
- methods for reading from and writing to ZIP and GZIP files.
- methods for reading from and writing to JAR (Java Archive) files.
- these packages deal with image I/O, including the encoding/decoding
of images in particular formats.
Java I/O Challenges
Unfortunately, when one first encounters Java I/O,
it seems to violate a primary goal of Java - try to be simpler
and more transparent code than the C and C++ languages! The
elegant abstraction that is so powerful when dealing with complex
I/O tasks can seem clumsy and overly complicated for basic tasks,
such as obtaining input from the console.
So far in this course, we skipped much of this confusion
by simply sending text output to the console with the System.out.println()
method. This static method uses the System.out
"standard" output stream, which the JVM opens by default;
no user setup is required. The System.out
object is an instance of PrintStream,
which provides fairly high level methods. There is also a standard
input stream, the System.in
class. However, it only provides low level methods that require
several steps to convert a console input to a string.
As we discussed in Chapter
5: Tech section, the PrintStream
class now has a printf()
method that works in a manner quite like a function of the same
name in C/C++. The new method uses internally an instance of the
class, added in J2SE5.0, that
provides C/C++ style numerical formatting capabilities. The printf()and
also the class Scanner,
used for reading formatted input, help considerably with simplifying
and expanding the range of basic I/O tasks.
We discuss these new tools in this chapter. We provide
lots of examples of other types of I/O tools and techniques as well,
so that one can find a template for whatever I/O task you need to
References & Web
Latest update: Nov. 10, 2004