简体   繁体   中英

difference between system.out and system.err

As I know both out and err are of same class PrintStream . Can anybody tell me how they differ...how they changed their behaviour?

The difference is not evident because by default in most of the Operating Systems they are written to the console (same file, console is also a file). However, you can have System.out write to a file, and System.err write to the console (monitor) - this is just one scenario.

Write a program that emits both System.out and System.err messages, and try this:

java MyProgram > out.txt 2> err.txt # On a *NIX.

System.out messages will go to out.txt and System.err messages will to err.txt. Basic point to remember is to think of System.out and System.err as streams to files (which is what they are) instead of a mechanism to output on the monitor, which is what I assumed as a beginner.

They go to the system stdout and stderr streams respectively. On most OSes these are distinct and can be sent to different places. For example, this might be useful if your program's output was to be parsed by another program - if it needed to report an error, stderr would normally be the better place for it as you might have set it up to get a human's attention.

They have the same behavior. But first (out) is a reference to standard output stream (by default it's a console). And second (err) is a reference to standard error stream (by default it's a console too).

But if you want, you can change a reference, or you can add a wrapper/filter to each of them.

My IDE, for example, shows output from err stream in red colors.

System.out sends the output to the standard output stream. System.err sends the output to the standard error stream. By default both of these write to the console.

However the benefit is that the two stream can be redirected so you could have the system.out output redirected to your normal log file and you could have the System.err output redirected to an error log.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM