java 標準輸出與標準錯誤 out與 err 區別 用法 聯絡 java中的out與err區別 System.out和System.err的區別 System.out.println和System.err.println的區別 Java重新導向System.out和System.err

來源:互聯網
上載者:User

標籤:gdc   lcm   key   kvo   psr   xdp   fan   otl   ipa   

作業系統一般都有三個標準檔案描述符:標準輸入,標準輸出,標準出錯

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

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

比如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 ")))); 

重新導向後沒有輸出了

java 標準輸出與標準錯誤 out與 err 區別 用法 聯絡 java中的out與err區別 System.out和System.err的區別 System.out.println和System.err.println的區別 Java重新導向System.out和System.err

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.