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:

When you use JDK 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 public static variables 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) 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:

Compile and run the program with the following command:

herong> javac Reverser.java

herong> java -classpath . Reverser

Then enter 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 see 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:

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:

herong> java -classpath . 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

Table of Contents

 About This Book

 JDK - Java Development Kit

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

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 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

 Java Modules - Java Package Aggregation

 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

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB