Java Reference
In-Depth Information
sion reads input into
data
beginning at the location specified by
start
. Up to
max
bytes are
stored. It returns the number of bytes read, or -1 when the end of the stream is reached.
All throw an
IOException
when an error occurs. When reading from
System.in
, pressing
ENTER
generates an end-of-stream condition.
Here is a program that demonstrates reading an array of bytes from
System.in
. Notice
that any I/O exceptions that might be generated are simply thrown out of
main( )
. Such
an approach is common when reading from the console, but you can handle these types of
errors yourself, if you choose.
Here is a sample run:
Writing Console Output
As is the case with console input, Java originally provided only byte streams for console
output. Java 1.1 added character streams. For the most portable code, character streams are
recommended. Because
System.out
is a byte stream, however, byte-based console output