该代码可以实现让System.out.print输出内容不再打印到控制台,而是输出到指定的文件中
<strong><span style="font-size:24px;">package com.zken.testcase;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class TestSystemOut {
public void redirectSystemOut() {
try {
System.setOut(new PrintStream(new FileOutputStream("systemOut.txt")));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return;
}
System.out.println("Hello World!");
}
public static void main(String[] args) {
new TestSystemOut().redirectSystemOut();
}
}</span></strong>
转载于:https://www.cnblogs.com/iamconan/p/7383594.html