Java Standard Streams

As we know, all Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of the run-time environment. For example, using some of its methods, we can obtain the current time and the settings of various properties associated with the system. System class also contains three predefined stream variables, in, out, and err. These fields are declared as public and static within System. This means that they can be used by any other part of our program and without reference to a specific System object. 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 typically are used to read and write characters from and to the console. We can wrap these within character-based streams, if desired. The preceding chapters have been using System.out in their examples. We can use System.err in much the same way.

  • Standard Input: Accessed through in which is used to read input from the keyboard.
  • Standard Output: Accessed through out which is used to write output to the display (console).
  • Standard Error: Accessed through err which is used to write error output to the display (console).

These objects are defined automatically and do not need to be opened explicitly. Standard Output and Standard Error, both are to write output; having error output separately so that the user may read error messages efficiently.

System.in is a byte stream that has no character stream features. To use Standard Input as a character stream, wrap System.in within the InputStreamReader as an argument.

InputStreamReader input = new InputStreamReader(System.in);

 

Using Console Input

In Java, console input is accomplished by reading from System.in. To obtain a character-based stream that is attached to the console, we wrap System.in in a BufferedReader object, to create a character stream. BuffereredReader supports a buffered input stream. Its most commonly used constructor is shown here:

BufferedReader(Reader inputReader);

Here, inputReader is the stream that is linked to the instance of BufferedReader that is being created. Reader is an abstract class. One of its concrete subclasses is InputStreamReader, which converts bytes to characters. To obtain an InputStreamReader object that is linked to System.in, use following constructor:

InputStreamReader(InputStream inputStream);

Because System.in refers to an object of type InputStream, it can be used for inputStream. Putting it all together, the following line of code creates a BufferedReader that is connected to the keyboard:

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

After this statement executes, input is a character-based stream that is linked to the console through System.in.

 

 

In the next section, we will talk about the Java Files and related API.