java 標准輸出與標准錯誤 out與 err 區別 用法 聯系 java中的out與err區別 System.out和System.err的區別 System.out.println和System.err.println的區別 Java重定向System.out和System.err


本文關鍵詞:

java 標准輸出與標准錯誤    out與 err 區別 用法 聯系  java中的out與err區別  System.out和System.err的區別 System.out.println和System.err.println的區別 Java重定向System.out和System.err

概述

操作系統一般都有三個標准文件描述符:標准輸入,標准輸出,標准出錯

這是操作系統的一種抽象表達

不同的語言需要有不同的具體表達方式,當然也不過是另一種包裝抽象

比如c++的  cin cout cerr

Java中則是的System.in,System.out,System.err


示例

輸出結果:

----------------

----------------

可以看得出來:

運行多次  err的打印信息位置是不固定的


JDK文檔

 
 /**
     * 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 static final 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 static final PrintStream err = null;

是System 的兩個內置變量   都是 PrintStream  類型的

 

out:

“標准”輸出流。此流已打開並准備接受輸出數據。

    通常,此流對應於顯示器輸出或者由主機環境或用戶指定的另一個輸出目標。

err:

“標准”錯誤輸出流。此流已打開並准備接受輸出數據。

    通常,此流對應於顯示器輸出或者由主機環境或用戶指定的另一個輸出目標。

    按照慣例,此輸出流用於顯示錯誤消息

    或者顯示那些即使用戶輸出流(變量 out 的值)已經重定向到通常不被連續監視的某一文件或其他目標,也應該立刻引起用戶注意的其他信息。

也就是說,out用於輸出,err用於一切你認為邏輯上是錯誤的東西,需要引起注意的東西

 


差別

System.out在JVM和操作系統都具有緩存功能,

就是你輸出的東西不一定實時輸出,有時候會積攢到一定數量才會輸出

System.err實時輸出(默認設置,可以改)

這也是為什么err打印位置不固定的原因

 

如果使用了log4j的日志記錄,且設定錯誤等級的話  System.err會被記入日志,System.out不會

而且一般在IDE中使用err ,都會變色的比如eclipse中紅色

System.setErr()System.setOut() 可以重定向這兩個流

System.setOut(new PrintStream(new FileOutputStream(new File( "d://out.txt ")))); System.setErr(new PrintStream(new FileOutputStream(new File( "d://err.txt ")))); 

重定向后沒有輸出了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM