将java输出打印到文件

Lon*_*don 2 java

如何在一些文件中同时将Java输出打印到shell控制台?那可能吗?

How*_*ard 6

您可以在程序开头包含以下行:

final PrintStream origout = System.out;
final PrintStream fileout = new PrintStream(file);
System.setOut(new PrintStream(new OutputStream() {
    @Override
    public void write(int b) throws IOException {
        origout.write(b);
        fileout.write(b);
    }
}));
Run Code Online (Sandbox Code Playgroud)