将java输出打印到文件 [英] Print java output to a file

查看:133
本文介绍了将java输出打印到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to print java output to both shell console and at the same time in some file? is that possible?

推荐答案

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

You can include the following lines at the start of your program:

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

这篇关于将java输出打印到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆