【问题标题】:I'm successfully exporting my console output to a file. I need to stop it and then print some data on console in java?我已成功将控制台输出导出到文件。我需要停止它,然后在 java 的控制台上打印一些数据?
【发布时间】:2021-10-03 01:59:34
【问题描述】:

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

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);
    }

【问题讨论】:

  • 你试过System.setOut(System.out)吗?
  • 是的,我试过了,但效果不佳。我什至尝试过 myconsole.close() ,它不会在文件中打印数据,也不会在控制台上打印。对于控制台,我至少需要它

标签: java arrays eclipse selenium java-io


【解决方案1】:

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

PrintStream prevConsole = System.out;

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

然后使用以下命令恢复到原始控制台:

System.setOut(prevConsole);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 2020-04-24
    • 2022-12-04
    • 2021-06-20
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多