Standard Input and Standard Output

What is in the Java 'System' class? What is System.out? What is System.in?

The System class has three static fields (in, out and err) and a number of static methods. There are no constructors for the System class and you cannot create an instance of the System class by yourself. Rather, an instance of the object is created for you by the Java runtime environment and is available to your application when it begins execution.
System.out is an instance of the PrintStream class (i.e., a PrintStream object) created by the Java runtime environment before your Java application begins execution. This output stream is connected to your console by default, but you can redirect it by providing the appropriate command line arguments when you run your program.
System.in is an instance of the InputStream class (an InputStream object) created by the Java runtime environment before your Java application begins execution. This input stream is connected to your keyboard by default, but you can redirect it to a different input source by the use of command line arguments.
System.err, like System.out, is an instance of the PrintStream class (i.e., a PrintStream object) created by the Java runtime environment before your Java application begins execution. It is also connected to your console by default, and is redirectable.

What can a PrintStream object (like System.out or System.err) do? What methods does it have?

A PrintStream object is an easy-to-use tool for outputting data in human-readable form. As the Java API states, a PrintStream never throws an IOException so you don't have to worry about handling output errors. It also implements a number of print(…), println(…) and printf(…) methods. All characters printed by a PrintStream are converted into bytes using the platform's default character encoding.

What can an InputStream object (like System.in) do? What methods does it have?

InputStream is one of Java's abstract classes which must be extended in order to be useful. The InputStream object assigned to your System.in variable has been extended in some fashion. The actual type of InputStream object you get depends on how (or if) you have redirected your standard input but it will, in general, some an instance of some class that extends the InputStream class. Input from a file is read by a FileInputStream object while keyboard input is dealt with differently. Whatever the case, an InputStream object only gives you the capability of reading bytes (single bytes or arrays of bytes) from an input stream.

I want to read characters but my InputStream object can only read bytes. What should I do?

Enlist the services of an InputStreamReader object. The Java API says that it a bridge from byte streams to character streams. It reads bytes and decodes them into characters in a character set that you can specify. Just pass your InputStream object as a parameter to the InputStreamReader constructor and it will return an InputStreamReader to you. Then you can use the InputStreamReader's read(…) methods to read single characters or arrays of characters. If you're going to do this, however, you need to tell Java what you are going to do if an I/O error occurs. You need to know the basics of exception handling in order to do this, or you can simply say (in your method signature) that you are throwing IOExceptions and let your program die if one occurs.

I don't know how many characters have been entered from the keyboard. How can I read an entire line of input?

As you read characters one-at-a-time, you can be watching for the end-of-line character. When you see it come in, you will know that you have read an entire line of input. An easier way is to let a BufferedReader do this for you. Create a new BufferedReader, passing your InputStreamReader as a parameter to the constructor for this class. Then simply use the BufferedReader's readLine() method to get an entire line of input returned to you as a String object. As with InputStreamReaders, you need to let Java know what you want to do if an I/O error occurs.

Things to try
  1. Write a program that outputs to both standard output and standard error. Use command line options to redirect the standard output and standard error to two different text files.
    java myprog  >stdout.txt  2>stderr.txt 
    

© Wiggen & Associates, 2007