![]() ![]() ![]() ![]() |
Input and Output Streams |
You have probably usedSystem.out.println()
to display information to the user, and you may have usedSystem.in.read()
to read in characters entered by the user. If you have used these methods, then you have, perhaps unknowingly, been using input and output streams from the java.io package. These streams are created and managed by the Systemclass and implement the standard streams.
The concept of standard input and output streams is a C library concept that has been assimilated into the Java environment. There are three standard streams all of which are managed by the java.lang.System class:
- standard output--referenced by
System.out
- used for program output, typically displays information to the user.
- standard input--referenced by
System.in
- used for program input, typically reads input entered by the user.
- standard error--referenced by
System.err
- used to display error messages to the user.
Standard Output and Standard Error
Probably the most often used items from the System class are the streams used for writing program output--the standard output and standard error streams. Both the standard output and standard error streams are System class variables; you can reference them withSystem.out
andSystem.err
, respectively.The example program in The Nuts and Bolts of the Java Language uses the standard output stream to display the results of the program to the user. Visit The Standard Output Stream
in that lesson for a short discussion about the standard output stream.
Both the standard output and standard error streams are instances of the PrintStream
class from the java.io package. The PrintStream class implements an output stream that is easy to use. Simply call one of the
print()
,println()
, orwrite()
methods to write various types of data to the stream.PrintStream is one of a set of streams known as filtered streams that are covered in IOLINK later in this lesson.
Standard Input
In addition to the two output streams for writing data, the System class provides a stream for reading data--the standard input stream--referenced withSystem.in
. The example program in The Nuts and Bolts of the Java Language uses the standard input stream to count the number of characters entered by the user. Visit The Standard Input Streamin that lesson for a short discussion about the standard input stream.
The standard input stream is an InputStream
. InputStream which is an abstract base class from which all input streams in the java.io package derive. InputStream defines a programming interface for input streams that includes methods for reading from the stream, marking a location within the stream, skipping to a mark, and closing the stream.
See also
java.lang.InputStream
java.lang.PrintStream
java.lang.System
![]() ![]() ![]() ![]() |
Input and Output Streams |