Java Reference
In-Depth Information
A stream is an abstract representation of an input or output device that is a source of, or destination for,
data. The stream concept enables you to transfer data to and from diverse physical devices such as disk files,
communications links, or just your keyboard, in essentially the same way.
In general you can write data to a stream or read data from a stream. You can visualize a stream as a
sequence of bytes that flows into or out of your program. Figure 8-1 illustrates how physical devices map to
streams.
FIGURE 8-1
Input and Output Streams
A stream to which you can write data is called an output stream . The output stream can go to any device
to which a sequence of bytes can be transferred, such as a file on a hard disk, or a network connection. An
output stream can also go to your display screen, but only at the expense of limiting it to a fraction of its
true capability. Stream output to your display is output to the command line. When you write to your display
screen using a stream, it can display characters only, not graphical output. Graphical output requires more
specialized support that I discuss from Chapter 17 onward.
NOTE Notethatalthoughaprintercanbeconsiderednotionallyasastream,printinginJava
does not work this way. A printer in Java is treated as a graphical device, so sending output
to the printer is very similar to displaying graphical output on your display screen. You learn
how printing works in Java in Chapter 21.
You read data from an input stream . In principle, this can be any source of serial data, but is typically a
disk file, the keyboard, or a connection to a remote computer.
Under normal circumstances, file input and output for the machine on which your program is executing
is available only to Java applications . It's not available to Java applets except to a strictly limited extent. If
 
 
Search WWH ::




Custom Search