Java 中 System.err.println() 的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5630784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:59:55  来源:igfitidea点击:

Use of System.err.println() in Java

java

提问by Harry Joy

On standard console all things are printed in white whether we have written it in System.outor System.err. In IDE(for me Eclipse) we can see different color output on console for both. i.e. black for System.outand red for System.err.

在标准控制台上,无论我们是用System.out还是System.err. 在 IDE(对我来说 Eclipse)中,我们可以在控制台上看到两者的不同颜色输出。即黑色代表System.out和红色代表System.err

Is System.erris only provided for use in IDEs? Cause on cmd we can not distinguish System.outand System.err. Both are printed in same color.

是否System.err仅提供在 IDE 中使用?因为在 cmd 上我们无法区分System.outSystem.err。两者均以相同颜色印刷。

回答by oiavorskyi

These are two different output streams that are available in most of OS's. You don't have them color coded due to settings of your terminal/command line environment. On the other hand your IDE provides different visualization for different streams.

这是大多数操作系统中可用的两种不同的输出流。由于终端/命令行环境的设置,您没有对它们进行颜色编码。另一方面,您的 IDE 为不同的流提供不同的可视化。

If you wanted to color them, consider using ANSI escape sequences.

如果您想为它们着色,请考虑使用 ANSI 转义序列。

回答by WhiteFang34

System.outgoes to the standard output stream (stdout) and System.errgoes to the standard error stream (stderr). See standard streamsfor details and how you can control where they go. Eclipse just conveniently colour codes them for you so you can distinguish them in one view.

System.out转到标准输出流 (stdout) 和System.err转到标准错误流 (stderr)。有关详细信息以及如何控制它们的去向,请参阅标准流。Eclipse 只是方便地为您对它们进行颜色编码,以便您可以在一个视图中区分它们。

回答by Dead Programmer

From system-in-out-error:

系统输入错误

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.

System.err 是一个 PrintStream。System.err 的工作方式与 System.out 类似,但它通常仅用于输出错误文本。某些程序(如 Eclipse)会以红色文本显示 System.err 的输出,以使其更明显是错误文本。

From JLS:

来自JLS

20.18.3 public static PrintStream err;

The initial value of this variable is a "standard" error output stream, already open and ready to accept output data. Typically, this corresponds to display output or another output destination specified by the host environment or user. By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variable out, has been redirected to a file or other destination that is typically not continuously monitored. Note that this field is not final, so its value may be updated if necessary.

这个变量的初始值是一个“标准”错误输出流,已经打开并准备好接受输出数据。通常,这对应于主机环境或用户指定的显示输出或另一个输出目的地。按照惯例,此输出流用于显示错误消息或其他应引起用户立即注意的信息,即使主要输出流(变量 out 的值)已重定向到文件或其他目标通常不会持续监控。请注意,此字段不是最终字段,因此如有必要,可能会更新其值。

回答by Daniel

This is a relict from the unix world, where most functionality is available as unix commands which were intended to be chained. The output of one command is used to feed another like here:

这是 unix 世界的遗物,其中大多数功能都可用作旨在链接的 unix 命令。一个命令的输出用于提供另一个命令,如下所示:

grep -i 'token' file | mail [email protected]

The pipe symbol only redirects the stdout (System.out), but not the stderr (System.err). So error messages would be seen on the console, and the regular output would go to the mail command.

管道符号仅重定向 stdout (System.out),而不重定向 stderr (System.err)。因此将在控制台上看到错误消息,并且常规输出将转到邮件命令。

If there were just one stream, one could not distinguish between them.

如果只有一个流,人们无法区分它们。

Windows, not relying on the command line (This changed in Windows Server 2008!) didn't invent again but just took the unix concepts and made them available in their dos commands, too. It is just that nearly no Windows only users usually know what they are good for.

Windows,不依赖命令行(这在 Windows Server 2008 中发生了变化!)并没有再次发明,而是采用了 unix 概念,并在它们的 dos 命令中提供了它们。只是几乎没有 Windows 用户通常知道他们有什么好处。

回答by Cameron Skinner

Both System.outand System.erralways exist in Java.

双方System.outSystem.err始终在Java中存在。

Depending on your console it mightbe possible to get it to display the two streams in a different colour.

根据您的控制台,可能可以让它以不同的颜色显示两个流。

回答by arush436

Example use:

使用示例:

try {
    Class.doSomething(myFile);
  } catch (Exception e){
    System.err.println("Fatal error performing doSomething: " + e);
    System.exit(-1);
  }