The java.lang.System class, having three static members (in, out and err) that pre-initialize at JVM startup that’s what no need to initialize. These classes also known as Stream classes.
Out of these System.out is most frequent use by programmers to write output on the console.
System Stream Classes
Here is a list of System Stream classes objects:
System.in is an InputStream type object which is typically connected to the keyboard input of console programs.
Note: It’s generally used often, Mainly used by beginner-level programmers to insert input from the keyboard. Because when you consider for application-level data is commonly passed to program by:
- Command-line arguments
- Configuration files
- Application GUI
System.out is a PrintStream type used to outputs the data to the console.
Note: It’s the most frequent used Stream object to print out of the program and for debugging about the flow and values.
System.err is a PrintStream type mainly used to output an error texts to console.
Note: Some of IDE like eclipse show System.err output in the console in red color so that you can easily see error messages.
Example for System .in, System.out and System.err
In this example, you will see, How to insert value to a program by the keyboard by System.in. Output to console by System.out and for any error print by System.err.
Exchanging System Streams
By default System Stream class is to console, but we can also change System Streams by programmatical configuration for each type so that InputStream for System.in or OutputStream for System.out or System.err will read and write to the new stream.
- System.setIn() : This method use to change input stream.
- System.setOut() : This method use to change output stream.
- System.setErr() : This method use to change error output stream.
System.out Stream Change
In the below example, out will be got to the given file instead of default OutputStream as Console.
OutputStream output = new FileOutputStream("c:\\output\\FacingIssuesOnIT_logs.txt"); PrintStream printOut = new PrintStream(output); //This method will change output stream. System.setOut(printOut);