Every language has some predefined streams for its users and Java is one of these languages. Three Java Predefined streams or standard streams are available in the java.lang.System class. These are as follows:

System.in  This is the standard stream for input. This stream is used for reading data for the program from the keyboard by default.
System.out This is the standard stream for output. This stream is used for writing data from the program to an output device such as a monitor / console by default or to some specified file.
System.err  This is a standard stream for error. This is used to show an error message on the screen i.e. console by default for the users.

System.in is an object of InputStream. On the other hand, System.out and System.err are both an object of type PrintStream.

All these Java Predefined Streams are automatically initialized by Java’s JVM (Java Virtual Machine) on startup. All these streams are byte streams but these are used to read and write character streams as well.

Let us have a close look at these streams one at a time.

Java Predefined Stream for Standard Input

System.in is the stream used for standard input in Java. This stream is an object of InputStream stream. In Java you need to wrap the System.in in the BufferedReader object in order to obtain a character based stream.

This is done with the following code:

BufferedReader br = new BufferedReader (new InputStreamReader (System.in) );

Here, InputStreamReader is the input stream you are creating and br is the character based stream that will be linked through System.in. Now, br object can be used for reading inputs from users.

In order to understand how this stream is used, let us have a look at an example.

Program to show use of System.in of  Java Predefined Streams

 
 
  1. import java.io.*;
  2. class InputEg
  3. {
  4.             public static void main( String args[] ) throws IOException
  5.             {
  6.                       String city;
  7.                       BufferedReader br = new BufferedReader ( new InputStreamReader (System.in) );
  8.                       System.out.println ( “Where do you live?" );
  9.                       city = br.readLine();
  10.                       System.out.println ( “You have entered " + city + " city" );
  11.             }
  12. }

Output:

Java Predefined Streams

This stream is a bit complicated so it is used less often as inputs are either passed on command line or are passed via GUI depending on the applications. In many console applications and applets, the job of System.in is done by using System.out itself.

Java Predefined Stream for Standard Output

System.out is an object of the PrintStream stream. This stream is the stream used for standard output in Java. The output of this stream is directed to the console by default by the program.  Have a look at the above example.

The statement System.out.println( “You have entered ” + city + ” city” ); of the code will direct the output of the program on the console of the user.

Java Predefined Stream for Standard Error

System.err is the stream used for standard error in Java. This stream is an object of PrintStream stream.

System.err is similar to System.out but it is most commonly used inside the catch section of the try / catch block. A sample of the block is as follows:

try {

// Code for execution

}

catch ( Exception e) {

System.err.println ( “Error in code: ” + e );