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.