Java Reference
In-Depth Information
11.6 I/O Exceptions
Processing input and output is a task that often produces unforeseeable situa-
tions, leading to exceptions being thrown. Let's explore some I/O issues and the
problems that may arise.
A stream is an ordered sequence of bytes. The term stream comes
from the analogy that as we read and write information, the data
flows from a source to a destination (or sink ) as water flows down
a stream. The source of the information is like a spring filling the
stream, and the destination is like a cave into which the stream flows.
In a program, we treat a stream as either an input stream , from which we read
information, or as an output stream , to which we write information. That is, a
program serves either as the spring filling the stream or as the cave receiving the
stream. A program can deal with multiple input and output streams at one time.
A particular store of data, such as a file, can serve either as an input stream or as
an output stream to a program, but it generally cannot be both at the same time.
There are three streams that are referred to as the standard I/O
streams . They are listed in Figure 11.2. The System class contains
three object reference variables ( in , out , and err ) that represent the
three standard I/O streams. These references are declared as both
public and static, which allows them to be accessed directly through
the System class.
We've been using the standard output stream, with calls to System.out.
println for instance, in examples throughout this topic. We've also used the
standard input stream to create a Scanner object when we want to process
input read interactively from the user. The Scanner class manages the input
read from the standard input stream in various ways that makes our program-
ming tasks easier. It also processes various I/O exceptions internally, creating an
InputMismatchException when needed.
The standard I/O streams, by default, represent particular I/O devices. System.in
typically represents keyboard input, whereas System.out and System.err typi-
KEY CONCEPT
A stream is a sequential sequence of
bytes; it can be used as a source of
input or a destination for output.
KEY CONCEPT
Three public reference variables in
the System class represent the stan-
dard I/O streams.
Standard I/O Stream
Description
System.in
Standard input stream.
System.out
Standard output stream.
System.err
Standard error stream (output for error messages)
FIGURE 11.2 Standard I/O streams
 
Search WWH ::




Custom Search