我成功地将我的控制台输出导出到一个文件。我需要停止它然后在 java 中在控制台上打印一些数据吗?

I'm successfully exporting my console output to a file. I need to stop it and then print some data on console in java?

我正在成功地将我的控制台输出导出到一个文件中。我现在需要结束这个过程,以便可以在控制台上打印剩余的数据。目前,所有打印的报表正在导出到文件中。

try {
        PrintStream myconsole = new PrintStream(new File("C:\one\Chase-"+formattedDate+".txt"));
        System.setOut(myconsole);
        myconsole.print(sb);
        
    } catch (FileNotFoundException fx) {
        System.out.println(fx);
    }

您应该在更改之前捕获原始控制台:

PrintStream prevConsole = System.out;

调用 System.setOut(myconsole); 后,您可以使用 System.out.print / println 代替 myconsole.print / println

之后恢复到原始控制台:

System.setOut(prevConsole);

然后不要忘记使用 myconsole.close() 在适当的地方关闭您的文件。