Java.lang.System.setErr() 方法
描述
java.lang.System.setErr() 方法重新分配"标准"错误输出流。
声明
以下是 java.lang.System.setErr() 方法的声明。
public static void setErr(PrintStream err)
参数
err − 这是新的标准错误输出流。
返回值
此方法不返回任何值。
异常
SecurityException − 如果存在安全管理器并且其 checkPermission 方法不允许重新分配标准错误输出流。
示例
下面的例子展示了 java.lang.System.setErr() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class SystemDemo {
public static void main(String[] args) throws Exception {
// create a file
FileOutputStream f = new FileOutputStream("file.txt");
System.setErr(new PrintStream(f));
// redirect the output
System.err.println("This will get redirected to file");
}
}
假设我们有一个文本文件 file.txt,它作为示例程序的输出生成。文件内容包括 −
This will get redirected to file

