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, HerongYang.com, All Rights Reserved.
 */
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());
         }
      }
   }
}

Here is my test session with JDK 1.7.0 on my Windows 7 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)

The test result tells me that:

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)

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

Conclusions:

Note that when I tested the same program with JDK 1.6.0 on my Windows XP system in 2010, the program responded differently on Ctrl-C input:

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 Ctrl-C did not terminate the application immediately. It caused the readLine() returning a null, which throws an exception on the Double.parseDouble(line) call.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version