Java Streams  «Prev 

Java Streams - Shells in Java IO

Shells and Consoles

The first source of input most programmers encounter is System.in. This is the same thing as the "stdin in C" which can be some sort of console window, most likely the one in which the Java program was launched. If the input is redirected so the program reads from a file, then System.in is changed as well. For instance, on Unix, the following command redirects stdin so that when the MessageServer program reads from System.in, the actual data comes from the file data.txt instead of the console:

% java MessageServer < data.txt

The console is also available for output through the static field out in the java.lang.System class, which is known as System.out. This is equivalent to "stdout in C" and may be redirected in a similar fashion. Furthermore, stderr is available as System.err. This is most commonly used for debugging and printing error messages from inside catch clauses.
For example:

try {
//... write code that might throw an exception
}
catch (Exception e) { 
  System.err.println(e); 
}

System.out and System.err PrintStreams

Both System.out and System.err are print streams, which are instances of java.io.PrintStream.
Shell is a term derived from the Unix world. It refers to a command line interface like the DOS prompt. It also refers to the program that interprets the commands the user types at the command line. In modern GUIs, like Windows and Gnome, it is not uncommon to have multiple shell windows open at one time. Different shells often understand a different set of commands.

PrintStream Class Hierarchy


java.io
Class PrintStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable
Direct Known Subclasses:
LogStream

The console is the particular shell or command line window where
  1. output from System.out goes and
  2. input from System.in is read.
It is normally the command line window from which the Java program is launched. Thus, two different Java programs may have different consoles on the same system.