Java System.out.println() 和 System.err.println() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3163399/
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
difference between System.out.println() and System.err.println()
提问by user354299
What is the difference between System.out.println()
and System.err.println()
in Java?
JavaSystem.out.println()
和System.err.println()
Java 中的区别是什么?
回答by Carlos G.
In Java System.out.println()
will print to the standard out of the system you are using. On the other hand, System.err.println()
will print to the standard error.
在 Java 中,System.out.println()
将打印到您正在使用的系统之外的标准。另一方面,System.err.println()
将打印到标准错误。
If you are using a simple Java console application, both outputs will be the same (the command line or console) but you can reconfigure the streams so that for example, System.out
still prints to the console but System.err
writes to a file.
如果您使用的是简单的 Java 控制台应用程序,则两个输出将相同(命令行或控制台),但您可以重新配置流,例如,System.out
仍然打印到控制台但System.err
写入文件。
Also, IDEs like Eclipse show System.err
in red text and System.out
in black text by default.
此外,默认情况下,Eclipse 等 IDESystem.err
以红色文本和System.out
黑色文本显示。
回答by Micha? Piaskowski
Those commands use different output streams. By default both messages will be printed on console but it's possible for example to redirect one or both of these to a file.
这些命令使用不同的输出流。默认情况下,这两条消息都将打印在控制台上,但例如可以将其中一个或两个重定向到一个文件。
java MyApp 2>errors.txt
This will redirect System.err
to errors.txt
file.
这将重定向System.err
到errors.txt
文件。
回答by onurbaysan
System.out
's main purpose is giving standard output.
System.out
的主要目的是提供标准输出。
System.err
's main purpose is giving standard error.
System.err
的主要目的是给出标准错误。
Look at these
看看这些
http://www.devx.com/tips/Tip/14698
http://www.devx.com/tips/Tip/14698
http://wiki.eclipse.org/FAQ_Where_does_System.out_and_System.err_output_go%3F
http://wiki.eclipse.org/FAQ_Where_does_System.out_and_System.err_output_go%3F
回答by Paul Richter
System.out is "standard output" (stdout) and System.err is "error output" (stderr). Along with System.in (stdin), these are the three standard I/O streams in the Unix model. Most modern programming environments (C, Perl, etc.) support this model.
System.out 是“标准输出”(stdout),System.err 是“错误输出”(stderr)。与 System.in (stdin) 一起,这些是 Unix 模型中的三个标准 I/O 流。大多数现代编程环境(C、Perl 等)都支持这种模型。
The standard output stream is used to print output from "normal operations" of the program, while the error stream is for "error messages". These need to be separate -- though in most cases they appear on the same console.
标准输出流用于打印程序“正常操作”的输出,而错误流用于“错误消息”。这些需要分开——尽管在大多数情况下它们出现在同一个控制台上。
Suppose you have a simple program where you enter a phone number and it prints out the person who has that number. If you enter an invalid number, the program should inform you of that error, but it shouldn't do that as the answer: If you enter "999-ABC-4567" and the program prints an error message "Not a valid number", that doesn't mean there is a person named "Not a valid number" whose number is 999-ABC-4567. So it prints out nothing to the standard output, and the message "Not a valid number" is printed to the error output.
假设您有一个简单的程序,您可以在其中输入电话号码并打印出拥有该号码的人。如果您输入一个无效号码,程序应该通知您该错误,但它不应该这样做作为答案:如果您输入“999-ABC-4567”并且程序打印一条错误消息“Not a valid number” ,这并不意味着有一个名为“无效号码”的人,其号码是 999-ABC-4567。因此,它不会向标准输出输出任何内容,并且会向错误输出输出消息“Not a valid number”。
回答by Artmal
It's worth noting that an OS has one queue for both System.err and System.out. Consider the following code:
值得注意的是,操作系统有一个用于 System.err 和 System.out 的队列。考虑以下代码:
public class PrintQueue {
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
System.out.println("out");
System.err.println("err");
}
}
}
If you compile and run the program, you will see that the order of outputs in console is mixed up.
如果编译并运行该程序,您会看到控制台中的输出顺序混乱。
An OS will remain right order if you work either with System.out or System.err only. But it can randomly choose what to print next to console, if you use both of these.
如果您只使用 System.out 或 System.err,操作系统将保持正确的顺序。但是如果你同时使用这两个,它可以随机选择在控制台旁边打印什么。
Even in this code snippet you can see that the order is mixed up sometimes:
即使在此代码片段中,您也可以看到顺序有时会混淆:
public class PrintQueue {
public static void main(String[] args) {
System.out.println("out");
System.err.println("err");
}
}
回答by Arth Tyagi
System.out.println("wassup");
refers to when you have to output a certain result pertaining to the proper input given by the user whereas System.err.println("duh, that's wrong);
is a reference to show that the input provided is wrong or there is some other error.
System.out.println("wassup");
指的是当您必须输出与用户给出的正确输入有关的某个结果时,而System.err.println("duh, that's wrong);
是表明所提供的输入错误或存在其他错误的参考。
Most of the IDEs show this in red color (System.err.print
).
大多数 IDE 以红色 ( System.err.print
)显示这一点。
回答by ATIQ UR REHMAN
this answer most probably help you it is so much easy
System.err
and System.out
both are the same both are defined in System
class as reference variable of PrintStream
class as
public final static PrintStream out = null;
and
public final static PrintStream err = null;
这个答案最有可能帮助你这是这么多的方便
System.err
和System.out
两者是相同的两个中定义System
类作为参考变量PrintStream
的类
public final static PrintStream out = null;
和
public final static PrintStream err = null;
means both are ref. variable of PrintStream
class.
normally System.err
is used for printing an error messages, which increase the redability for the programmer.
意味着两者都是参考。PrintStream
类的变量。通常System.err
用于打印错误消息,增加程序员的可红性。
A minor difference comes in both when we are working with Redirectionoperator.
当我们使用重定向运算符时,两者都有细微的差别。