简体   繁体   中英

Java: System.out and System.err output to the same stream?

When using System.out.println or System.err.println what is the difference? In a console app they seems to be practically the same.

Is there a way to change this?

The difference is defined in the underlying operating system. By default both of these streams are sent to the console (ie your monitor). However, a user can redirect them independently. For example in a Unix-like environment, java MyClass > output.txt will redirect System.out to the file output.txt , but anything sent to System.err will still go to the console. For more details, you can google for stdout and stderr ; these are the typical names for these streams on most operating systems.

The difference is clearly defined here:

http://docs.oracle.com/javase/6/docs/api/java/lang/System.html

There are also methods defined (which you will find in this document) for how to change these streams (System.setErr, System.setOut, System.setIn)

System.out is the standard output and System.err is the error output.

  • Some IDE Consoles show the System.out in standard font and System.out in red color.

  • A Java web server would send the err and out to two different files.

  • Using the command line java -jar yourjar.jar >out.txt 2>err.txt would send System.out to out.txt and System.err to err.txt

In addition to what everyone else here has said since they are two separate streams, interleaving of threads in a concurrent system could cause the print order to surprise you if you use them interchangeably. It just depends on how the operating system prioritizes them when telling the processor what to do.

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