這三個都是java中可以自動使用的流,System.out和System.err這兩個都可以用于輸出,而System.out用于正常的代碼的輸出,而System.err是用于輸出錯誤的提示信息。
在重新定向輸出數(shù)據(jù)的時候,擁有兩個不同的標(biāo)準(zhǔn)輸出流是方便的,比如說可以把正常的的輸出數(shù)據(jù)定向到一個文件中,而另外一個錯誤的數(shù)據(jù)則可以輸出到另一個文件中。
通過使用System類的靜態(tài)方法setIn,setOut,setErr來完成 我們看一個實例代碼
package com.li;import java.io.PRintStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;public class StringTokenizerDemo { public static void main(String[] args) { // TODO 自動生成的方法存根 PrintStream outputStream = null; try { outputStream = new PrintStream(new FileOutputStream("test.txt")); }catch(FileNotFoundException e) { System.out.println("Error opening File"); System.exit(0); } System.setErr(outputStream); System.out.println("Hello to screen"); System.err.println("Hello to File"); outputStream.close(); }}在屏幕上輸出的是Hello to screen 在文件中可以看到Hello to File
新聞熱點
疑難解答