static void setErr(PrintStream err)
描述 (Description)
java.lang.System.setErr()方法重新分配“标准”错误输出流。
声明 (Declaration)
以下是java.lang.System.setErr()方法的声明
public static void setErr(PrintStream err)
参数 (Parameters)
err - 这是新的标准错误输出流。
返回值 (Return Value)
此方法不返回任何值。
异常 (Exception)
SecurityException - 如果存在安全管理器且其checkPermission方法不允许重新分配标准错误输出流。
例子 (Example)
以下示例显示了java.lang.System.setErr()方法的用法。
package com.iowiki;
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