Java 在 System.setOut 之后写入真正的 STDOUT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3340342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:26:23  来源:igfitidea点击:

Writing to the real STDOUT after System.setOut

java

提问by Jim

I'm trying to intercept System.out and System.err, but maintain the ability to write to the original streams directly when necessary.

我试图拦截 System.out 和 System.err,但保持在必要时直接写入原始流的能力。

PrintStream ps = System.out;
System.setOut(new MyMagicPrintStream());
ps.println("foo");

Unfortunately, the details of the System class' implementation means that in my example, "foo" gets sent to MyMagicPrintStreaminstead of the real stdout.

不幸的是,System 类的实现细节意味着在我的例子中,“foo”被发送到MyMagicPrintStream而不是真正的stdout.

Does anyone know how to get references to the real/original OutputStreams?

有谁知道如何获得对真实/原始的引用OutputStreams

Thanks.

谢谢。

PS: This will otherwise result in a StackOverflowError <-- for SEO.

PS:否则这将导致 StackOverflowError <-- 对于 SEO。

采纳答案by fqsxr

try this:

尝试这个:

PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out))

回答by RealHowTo

Try something like this :

尝试这样的事情:

  PrintStream original = new PrintStream(System.out);

  // replace the System.out, here I redirect to NUL
  System.setOut(new PrintStream(new FileOutputStream("NUL:")));
  System.out.println("bar");  // no output

  // the original stream is still available 
  original.println("foo");  // output to stdout

回答by Eternal Noob

PrintStream original = System.out;
System.setOut(new MyMagicPrintStream());

// This will print to MyMagicPrintStream();
System.out.println("foo for MyMagicPrintStream");


System.setOut(original);

// This will print to original print stream;
System.out.println("foo for original print stream (stdout)");

This works for me.

这对我有用。

回答by Andrey Nudko

PrintStream original = new PrintStream(System.out);basically wraps the existing reference - so if System.setOut()was changing it - there should be no difference. That's probably an issue with particular JVM, but I would rather guess that something was wrong with the MyMagicPrintStream, or the code writing to the stdout. Actually the following piece of code does exactly what is expected on Sun 1.6.0_20-b02 for Windows:

PrintStream original = new PrintStream(System.out);基本上包装了现有的参考 - 所以如果System.setOut()正在改变它 - 应该没有区别。这可能是特定 JVM 的问题,但我更愿意猜测 MyMagicPrintStream 或写入标准输出的代码有问题。实际上,以下代码完全符合 Sun 1.6.0_20-b02 for Windows 上的预期:

import java.io.FileOutputStream;
import java.io.PrintStream;

public class SystemOutTest {

public static void main(String args[]) {
    try {
        PrintStream ps = System.out;
        System.setOut(new PrintStream(new FileOutputStream("stdout.log")));

        System.out.println("foo");  
        ps.println("bar");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

"foo" goes to stdout.log, "bar" to the console.

“foo” 进入 stdout.log,“bar” 进入控制台。

Also, if you need to access the original stdin / out assigned of JVM startup - you can use System.console();(just remember - it can be null!)

此外,如果您需要访问 JVM 启动时分配的原始标准输入 / 输出 - 您可以使用System.console();(请记住 - 它可以是null!)