我想写入一个文本文件。然后,我想继续打印到控制台。
我能够成功输出到文本文件。但是,在关闭为写入文本文件而创建的 PrintStream 后,我无法打印到控制台。例如,在下面的代码中,文本文件具有名为“permutation”的方法的输出,后跟短语“Test before close”。但是,“关闭后测试”短语既不会打印到文本文件,也不会打印到控制台。感谢您的任何建议!
public static void main(String[] args) throws FileNotFoundException {
PrintStream out2 = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out2);
permutation("1234");
System.out.println("Test before close");
out2.close();
System.out.println("Test after close");
}