Java Tutorials - Herong's Tutorial Examples - Version 7.00, by Dr. Herong Yang

Java Execution Console - "in", "out" and "err" Data Streams

This section provides a tutorial example on how to use default input and output data streams: System.in, System.out and System.err.

When the JVM is executing a Java application program, it needs a console window to provides 3 predefined input and output data streams to the Java program: to:

  • System.in: Standard input stream connected to the console window.
  • System.out: Standard output stream connected to the console window.
  • System.err: Standard error stream connected to the console window.

When you use J2SDK package to execute a Java program, the console window is the same window where you entered the "java" command.

Note that all 3 standard streams are defined as a public static variable of the "System" class. We have used the "System.out" in our first Java program, Hello.java, to print the hello message on the console window. Here is another program that uses all 3 standard streams:

/* Reverser.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
import java.io.*;
public class Reverser {
   public static void main(String[] arg) {
      InputStream in1 = System.in;
      InputStreamReader in2 = new InputStreamReader(in1);
      BufferedReader in = new BufferedReader(in2);
      PrintStream out = System.out;
      PrintStream err = System.err;
      err.println("This program reverses the character positions of ");
      err.println("each line from the standard input stream.");
      err.println("Enter '.' to end the program.");
      try {
         String s = in.readLine();
         while (s!=null) {
            char[] a = s.toCharArray();
            if (s.equals(".")) break;
            int n = a.length;
            for (int i=0; i<n/2; i++) {
               char c = a[i];
               a[i] = a[n-1-i];
               a[n-i-1] = c;
            }
            out.println(new String(a));
            s = in.readLine();
         }
      } catch (IOException e) {
         err.println(e.toString());
      }
   }
}

Note that:

  • The "System.in" is an "InputStream" object, which only offers methods to read in information as bytes. To be able to read as one "String" per line, I need to convert "System.in" to a "BufferedReader" object.
  • The "while" loop should end when "System.in" reaches the end of the stream. However, HotSpot for Windows gives you no way to indicate the end of the standard input stream from the key board. So I used a special pattern, one and only one character '.' in a line, to end the loop.

When I compiled and ran the program with the following command:

C:\herong>\progra~1\java\jdk1.8.0\bin\javac Reverser.java

C:\herong>\progra~1\java\jdk1.8.0\bin\java -cp . Reverser

Then entered the following text from key board:

Fish, I love you and respect you very much.
But I will kill you dead before this day ends.
.

I got the following on the console window:

This program reverses the character positions of
each line from the standard input stream.
Enter '.' to end the program.
Fish, I love you and respect you very much.
.hcum yrev uoy tcepser dna uoy evol I ,hsiF
But I will kill you dead before this day ends.
.sdne yad siht erofeb daed uoy llik lliw I tuB
.

Note that:

  • The first 3 lines are from the "System.err" stream.
  • The 4th, 6th, and 8th lines are copies of the text lines of the "System.in" stream, because the console window is echoing everything I typed in on the keyboard.
  • The 5th and 7th lines are from the "System.out" stream.

Windows system can also redirect the console input and output to files. First, I saved the 2 lines of text and the '.' into a file called input.txt, then run the following command:

C:\herong>\progra~1\java\jdk1.8.0\bin\java -cp . 
   Reverser < input.txt > output.txt

I only got the text from the "System.err" on the console window:

This program reverses the character positions of
each line from the standard input stream.
Enter '.' to end the program.

The output.txt file contains:

.hcum yrev uoy tcepser dna uoy evol I ,hsiF
.sdne yad siht erofeb daed uoy llik lliw I tuB

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

Execution Process, Entry Point, Input and Output

 Creating, Compiling and Executing Java Programs

 main() Method - Java Execution Entry Point

Java Execution Console - "in", "out" and "err" Data Streams

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version

Java Execution Console - "in", "out" and "err" Data Streams - Updated in 2014, by Dr. Herong Yang