Jenkov.com
Tutorials Books About
Java Io

1 Java IO Tutorial
2 Java IO Overview
3 Java IO: Files
4 Java IO: Pipes
5 Java IO: Networking
6 Java IO: Byte & Char Arrays
7 Java IO: System.in, System.out, and System.error
8 Java IO: Streams
9 Java IO: Input Parsing
10 Java IO: Readers and Writers
11 Java IO: Concurrent IO
12 Java IO: Exception Handling
13 Java IO: InputStream
14 Java IO: OutputStream
15 Java IO: FileInputStream
16 Java IO: FileOutputStream
17 Java IO: RandomAccessFile
18 Java IO: File
19 Java IO: PipedInputStream
20 Java IO: PipedOutputStream
21 Java IO: ByteArrayInputStream
22 Java IO: ByteArrayOutputStream
23 Java IO: FilterInputStream
24 Java IO: FilterOutputStream
25 Java IO: BufferedInputStream
26 Java IO: BufferedOutputStream
27 Java IO: PushbackInputStream
28 Java IO: SequenceInputStream
29 Java IO: DataInputStream
30 Java IO: DataOutputStream
31 Java IO: PrintStream
32 Java IO: ObjectInputStream
33 Java IO: ObjectOutputStream
34 Java IO: Serializable
35 Java IO: Reader
36 Java IO: Writer
37 Java IO: InputStreamReader
38 Java IO: OutputStreamWriter
39 Java IO: FileReader
40 Java IO: FileWriter
41 Java IO: PipedReader
42 Java IO: PipedWriter
43 Java IO: CharArrayReader
44 Java IO: CharArrayWriter
45 Java IO: BufferedReader
46 Java IO: BufferedWriter
47 Java IO: FilterReader
48 Java IO: FilterWriter
49 Java IO: PushbackReader
50 Java IO: LineNumberReader
51 Java IO: StreamTokenizer
52 Java IO: PrintWriter
53 Java IO: StringReader
54 Java IO: StringWriter




Java IO: System.in, System.out, and System.error


The 3 streams System.in, System.out, and System.err are also common sources or destinations of data. Most commonly used is probably System.out for writing output to the console from console programs.

These 3 streams are initialized by the Java runtime when a JVM starts up, so you don't have to instantiate any streams yourself (although you can exchange them at runtime).

System.in

System.in is an InputStream which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, or configuration files. In applications with GUI the input to the application is given via the GUI. This is a separate input mechanism from Java IO.

System.out

System.out is a PrintStream. System.out normally outputs the data you write to it to the console. This is often used from console-only programs like command line tools. This is also often used to print debug statements of from a program (though it may arguably not be the best way to get debug info out of a program).

System.err

System.err is a PrintStream. System.err works like System.out except it is normally only used to output error texts. Some programs (like Eclipse) will show the output to System.err in red text, to make it more obvious that it is error text.

Simple System.out + System.err Example:

Here is a simple example that uses System.out and System.err:

try {
  InputStream input = new FileInputStream("c:\\data\\...");
  System.out.println("File opened...");

} catch (IOException e){
  System.err.println("File opening failed:");
  e.printStackTrace();
}

Exchanging System Streams

Even if the 3 System streams are static members of the java.lang.System class, and are pre-instantiated at JVM startup, you can change what streams to use for each of them. Just set a new InputStream for System.in or a new OutputStream for System.out or System.err, and all further data will be read / written to the new stream.

To set a new System stream, use one of th emethods System.setIn(), System.setOut() or System.setErr(). Here is a simple example:

OutputStream output = new FileOutputStream("c:\\data\\system.out.txt");
PrintStream printOut = new PrintStream(output);

System.setOut(printOut);

Now all data written to System.out should be redirected into the file "c:\\data\\system.out.txt". Keep in mind though, that you should make sure to flush System.out and close the file before the JVM shuts down, to be sure that all data written to System.out is actually flushed to the file.



Connect with me: Newsletter - Get all my free tips!