標籤: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