0
点赞
收藏
分享

微信扫一扫

关于System.err和System.out

进击的包籽 2022-12-22 阅读 111


先看一段测试代码:

关于System.err和System.out_java

多运行几次,会发现每次的结果都不一样:

关于System.err和System.out_JVM_02

关于System.err和System.out_System_03

会发现输出的顺序有时候会不一样(要注意这里不是重排序,重排序是针对共享变量的)。

可以先参看官方文档的描述:

关于System.err和System.out_JVM_04

关于System.err和System.out_System_05

/**
* The "standard" output stream. This stream is already
* open and ready to accept output data. Typically this stream
* corresponds to display output or another output destination
* specified by the host environment or user.
* <p>
* For simple stand-alone Java applications, a typical way to write
* a line of output data is:
* <blockquote><pre>
* System.out.println(data)
* </pre></blockquote>
* <p>
* See the <code>println</code> methods in class <code>PrintStream</code>.
*
* @see java.io.PrintStream#println()
* @see java.io.PrintStream#println(boolean)
* @see java.io.PrintStream#println(char)
* @see java.io.PrintStream#println(char[])
* @see java.io.PrintStream#println(double)
* @see java.io.PrintStream#println(float)
* @see java.io.PrintStream#println(int)
* @see java.io.PrintStream#println(long)
* @see java.io.PrintStream#println(java.lang.Object)
* @see java.io.PrintStream#println(java.lang.String)
*/
public final static PrintStream out = null;

/**
* The "standard" error output stream. This stream is already
* open and ready to accept output data.
* <p>
* Typically this stream 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 <code>out</code>, has been redirected to a file or other
* destination that is typically not continuously monitored.
*/
public final static PrintStream err = null;

这个out和err虽然都是null,但是这两个流是打开状态的,是经过JVM调用底层I/O。

以下内容转自:​​http://love-love-l.blog.163.com/blog/static/21078304200811510346157/​​

1、System.out.println   能重定向到别的输出流,这样的话你在屏幕上将看不到打印的东西了,   
  而System.err.println只能在屏幕上实现打印,即使你重定向了也一样。
System.setOut(new   PrintStream(new   FileOutputStream(new   File( "c:/test.txt ")))); 
            System.out.println( "haha "); 
2、
当向控制台输出信息时,开发者有两个选择:System.out和System.err。使用者更倾向于输出的是System.out,而如果是 System.err则输出“error”。尽管这看起来是显而易见的,但很多开发者都不了解为什么出错和调试时使用System.err。     
  当输出一个流时,JVM和操作系统共同决定何时输出这个流。也就是说,尽管开发者键入了:   
  System.out.print_   
  ("Test   Output:");   
  JVM和操作系统的组合体并不会立即输出这个流。相反,它将保持等待状态直到将要输出的东西达到一定的量。   
  假设输入以下指令:   
  System.out.println("Debugging   Info.");   
  JVM可能同意输出;然而,操作系统可能决定暂不输出。   
  由于这个原因,在调试程序时想要发现出错的位置就有可能成为问题。考虑以下的程序:   
    
  for(int   i=0;   i<56;   i++)   {   
  System.out.println(i);   
  ...   //   containing   an   error   
  }   
  错误可能出现在i等于54时,但是可能JVM在i等于49时就结束输出了。50到54仍然存在于缓存中,结果也就丢失了。   
    
  使用System.err来报告错误、调试程序就可以避免这种情况出现,它将使每一次操作的结果都输出出来。例如以下程序:   
    
  for(int   i=0;   i<56;   i++)   {   
  System.err.println(i);   
  ...   //   containing   an   error   
  }   
  在每一次i等于54时都将显示错误信息。
3、System.out.println可能会被缓冲,而System.err.println不会
4、System.err和System.out   就是错误输出和标准输出 
如果你用LOG4J记录日志的话,且设定错误等级的话 
System.err的输出是将记录到日志中
5、输出设备是一样的   所以你看到的是一样的     
  System.setErr()   System.setOut()   是重定向两个流的方法。   
  以下为Sun   JDK1.5中文文档中的   可能有点泛泛了   
  ------------------------------   
  System.err   
  “标准”错误输出流。此流已打开并准备接受输出数据。   
    
  通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标。按照惯例,此输出流用于显示错误消息,或者显示那些即使用户输出流(变量   out   的值)已经重定向到通常不被连续监视的某一文件或其他目标,也应该立刻引起用户注意的其他信息。     
    
  System.out   
  “标准”输出流。此流已打开并准备接受输出数据。通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标。
6、System.err.println()是要缓冲的,所以优先级会高点,而System.out.println()是不需要缓冲的,所以优先级会低点.

举报

相关推荐

0 条评论