JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Standard Input, Output, and Error Streams

This section provides a tutorial example on how to use Standard Input, Output, and Error Streams provided by the java.lang.System class.

The first thing to learn about the java.lang.System class is using standard input, output and error streams provided as System.in, System.out, and System.err properties.

Here is tutorial example program testing standard input, output and error streams:

/**
 * SystemInOut.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
class SystemInOut {
   public static void main(String[] a) {

      // Standard input and output streams
      java.io.PrintStream out = System.out;
      java.io.InputStream sin = System.in;
      java.io.PrintStream err = System.err;

      // Converting the standard input stream into a reader
      java.io.BufferedReader in = 
        new java.io.BufferedReader(new java.io.InputStreamReader(sin));

      while (true) {
         // Writing to the standard output stream
         out.println("Please enter a number:");

         try {
            // Reading from the standard input reader
            String line = in.readLine();
            double input = Double.parseDouble(line);
            double output = Math.sqrt(input);
            out.println("SQRT("+input+") = "+output);

         } catch (Exception e) {
            out.println("Caught an exception. See the error message!");

            // Writing to the standard error stream
            err.println("Exception detail:");
            err.println(e.toString());
         }
      }
   }
}

The first test session is recorded with JDK 1.6.0 on my Windows XP system:

C:\herong\jvm>java SystemInOut
Please enter a number:
25
SQRT(25.0) = 5.0
Please enter a number:
4
SQRT(4.0) = 2.0
Please enter a number:
Four
Caught an exception. See the error message!
Exception detail:
java.lang.NumberFormatException: For input string: "Four"
Please enter a number:
(press Ctrl-C)
Caught an exception. See the error message!
Exception detail:
java.lang.NullPointerException
Please enter a number:

The test result tells me that:

  • On Windows systems, standard input, output, and error streams are all connected to the JVM console by default.
  • Pressing Ctrl-C does not terminate the application immediately.
  • Pressing Ctrl-C causes the readLine() returning a null, which throws an exception on the Double.parseDouble(line) call.

If you want to separate the standard error stream to a log file, you can use the Windows redirection symbol 2> as shown in the example below:

C:\herong\jvm>java SystemInOut 2> error.log
Please enter a number:
25
SQRT(25.0) = 5.0
Please enter a number:
4
SQRT(4.0) = 2.0
Please enter a number:
Four
Caught an exception. See the error message!
Please enter a number:
(press Ctrl-C)
Caught an exception. See the error message!
Please enter a number:

C:\herong\jvm>type error.log
Exception detail:
java.lang.NumberFormatException: For input string: "Four"
Exception detail:
java.lang.NullPointerException

Conclusions:

  • Standard error stream can be redirected to a file using 2> at the command line.
  • Standard input stream can be redirected from a file using < at the command line. You can test this easily.
  • Standard output stream can be redirected to a file using > at the command line. You can test this easily.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

java.lang.System Class - The Operating System

 What Is java.lang.System?

Standard Input, Output, and Error Streams

 Current Time in Milliseconds and Nanoseconds

 Accessing System Environment Variables

 Getting and Adding System Properties

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Standard Input, Output, and Error Streams