JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
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 how to use 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) 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 a test session of this sample program with HotSpot 13 on my macOS computer:
herong> 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:
herong> 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) herong> more error.log Exception detail: java.lang.NumberFormatException: For input string: "Four"
Takeaways:
Note that when I tested the same program with HotSpot 1.6 on my Windows XP system, the program responded differently on Ctrl-C input:
herong> 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.
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
►java.lang.System Class - The Operating 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
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
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