重置标准输出流 [英] Resetting Standard output Stream

查看:124
本文介绍了重置标准输出流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Java中有一个函数可以使用 System.setOut 方法将标准输出流设置为任何用户定义的值..

I know that there is a function in Java to set Standard Output Stream to any user defined value using System.setOut method..

但是有没有方法可以将标准输出重置为先前存储的输出或标准输出的输出?

But is there any method to reset the standard output to the one which was stored earlier or the one which is standard output?

推荐答案

您可以通过 FileDescriptor.out 。要重置标准输出以打印到控制台,你可以

You can get hold of the file descriptor for standard out through FileDescriptor.out. To reset standard out to print to console, you do

System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));

另一种方法是简单地保持原始对象,如下所示:

Another way is to simply hold on to the original object, as follows:

PrintStream stdout = System.out;
System.setOut(new PrintStream(logFile));

// ...

System.setOut(stdout);                   // reset to standard output

这篇关于重置标准输出流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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