这三个都是java中可以自动使用的流,System.out和System.err这两个都可以用于输出,而System.out用于正常的代码的输出,而System.err是用于输出错误的提示信息。
在重新定向输出数据的时候,拥有两个不同的标准输出流是方便的,比如说可以把正常的的输出数据定向到一个文件中,而另外一个错误的数据则可以输出到另一个文件中。
通过使用System类的静态方法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
新闻热点
疑难解答