将大量的system.out.println重定向到.txt文件 [英] Redirect lots of system.out.println's to a .txt file

查看:214
本文介绍了将大量的system.out.println重定向到.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


Java程序,我有一个很长的方法,(我不认为是重要的发布,因为它不是重要的问题)有大量的println语句来打印到控制台的状态更新。

我不希望将这些打印输出到控制台,而是希望它们进入一个txt文件,以便以后可以存储它们并进行检查。

有没有一种简单的方法来重定向输出,而无需手动通过每个println语句?



如果不是,最好的方法是什么?

解决方案

< System类有一个静态方法, System.setOut(PrintStream out)。所有你需要做的就是创建你自己的PrintStream,写入一个文件,把它塞进这个方法,你就全部设置好了。

更脆弱的解决方案:Don'不要使用System.out.printFoo(...),而应该使用一个记录器,并根据您的需要更改记录级别。


Possible Duplicate:
Print java output to a file

In a Java program, I have a long method, (which I don't think is important to post since it's not vital to the question) that has a large number of println statements to print status updates to the console.

Instead of having these printout to the console, I'd like them to go into a txt file where I can store them and review them later.

Is there a simple way to redirect the output without manually going through each println statement?

If not, what's the best way to go about this?

解决方案

The System class has a static method, System.setOut(PrintStream out). All you have to do is create your own PrintStream that writes to a file, stuff it into this method and you're all set.

Better less fragile solution: Don't use System.out.printFoo(...) but instead just use a logger, and change the logging levels as it suits your purposes at that time.

这篇关于将大量的system.out.println重定向到.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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