Java Streams  «Prev  Next»

Lesson 4 System.in and System.out
ObjectiveInvestigate the System.in and System.out input and output streams.

Using System.in and System.out

System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is the keyboard by default. System.err refers to the standard error stream, which also is the console by default. However, these streams may be redirected to any compatible I/O device. System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream. These are byte streams, even though they are typically used to read and write characters from and to the console. As you will see, you can wrap these within character-based streams, if desired. The preceding sections have been using System.out in their examples. You can use System.err in much the same way. As explained in the next section, use of System.in is a little more complicated.

Reading Console Input

In Java 1.0, the only way to perform console input was to use a byte stream. Today, using a byte stream to read console input is still acceptable. However, for commercial applications, the preferred method of reading console input is to use a character- oriented stream. This makes your program easier to internationalize and maintain. In Java, console input is accomplished by reading from System.in. To obtain a character-based stream that is attached to the console, wrap System.in in a BufferedReader object. BufferedReader supports a buffered input stream. A commonly used constructor is shown here:

BufferedReader(Reader inputReader)


Console: System.out, System.in, and System.err

The console is the default destination for output written to System.out or System.err and the default source of input for System.in. On most platforms the console is the command- line environment from which the Java program was initially launched, perhaps an xterm or a DOS shell window. The word console is something of a misnomer, since on Unix systems the console refers to a very specific command-line shell, rather than being a generic term for command-line shells overall. Many common misconceptions about I/O occur because your first exposure to I/O is through the console.
The console is a convenient solution for ad hoc scenarios and commonly found in textbooks. I will use the console for input and output demonstrations. It is really a very unusual
  1. source of input and
  2. destination for output,
and good Java programs should avoid it. It behaves almost, but not completely, unlike anything else you would want to read from or write to.
While consoles make convenient examples in programming web sites like this one, they are an awful user interface and have little place in modern programs. Users are more comfortable with a well-defined graphical user interface. Furthermore, the console is unreliable across platforms.

Most sources of data in a Java program are provided as input or output streams.
System.in and System.out are commonly used to communicate with the user in a character-mode environment and for debugging purposes.

System.in

System.in is a static field in the java.lang.System class. It is an input stream that is normally connected to the console, though some shells allow the user to redirect it to a file or other program.
In a Unix environment, you normally press Ctrl-D to generate the end-of-stream character on System.in. On most platforms, characters are only sent to System.in a line at a time, not as each character is typed. This allows the user to backspace over mistakes and correct them. Java does not allow you to put the console into a "raw" mode that sees each character, including backspaces and deletes, as it is typed. System.out is also a static field in the java.lang.System class. It is an output stream that is used to send data from the Java program to the console, though again the shell may allow the user to redirect it to a file.

AWT

Interestingly, the AWT is not a source of streams in Java.

Java Lambdas and Parallel Streams