System.out.println與System.err.println的區別(輸出順序!!!)


System.out.println與System.err.println的區別(輸出順序!!!)

 

System.out.println與System.err.println的區別(輸出順序!!!)

今天看到網上別人寫的代碼中有一行System.err.println用來輸出,以前從沒用過,今天一用出了很多問題,總結如下:

  • err是運行期異常和錯誤反饋的輸出流的方向
  • System.err.println只能在屏幕上實現打印,即使你重定向了也一樣
  • 用err打印出的 字符串,再eclipse的console會顯示成紅色
  • 標准輸出往往是帶緩存的,而標准出錯沒有緩存(默認設置,可以改)

參看以下代碼了解

下面輸出順序如何?


    1. public class TestCodeSeg 2. { 3. static 4. { 5. System.out.println("1"); 6. } 7. { 8. System.out.println("2"); 9. } 10. public TestCodeSeg() 11. { 12. System.err.println("3"); 13. } 14. public static void main(String[] args) 15. { 16. new TestCodeSeg(); 17. } 18. } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

結果:1,2順序不變,3輸出不定位置不定。

原因:System.out.println輸出有緩存,System.err.println是立即輸出,可能在輸出1或2,還沒有輸出換行時輸出3。

 


免責聲明!

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



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