【问题标题】:In Java, how can I redirect System.out to null then back to stdout again?在 Java 中,如何将 System.out 重定向到 null 然后再次返回到 stdout?
【发布时间】:2011-06-15 12:02:18
【问题描述】:

我尝试使用以下代码将 System.out 临时重定向到 /dev/null,但它不起作用。

System.out.println("this should go to stdout");

PrintStream original = System.out;
System.setOut(new PrintStream(new FileOutputStream("/dev/null")));
System.out.println("this should go to /dev/null");

System.setOut(original);
System.out.println("this should go to stdout"); // This is not getting printed!!!

有人有什么想法吗?

【问题讨论】:

  • 我在我的系统上看到两条线都很好。我正在使用 Java 6 更新 22。
  • 顺便说一句:当心在没有更广泛的同步的情况下像这样操纵 System.out 以防止多个线程的并发交互。
  • 从JDK11开始,内置了一个nullOutputStream。见my answer below

标签: java stdout iostream


【解决方案1】:

伙计,这不太好,因为 Java 是跨平台的,而 '/dev/null' 是 Unix 特定的(显然 Windows 上有替代方案,请阅读 cmets)。所以你最好的选择是创建一个自定义的 OutputStream 来禁用输出。

try {
    System.out.println("this should go to stdout");

    PrintStream original = System.out;
    System.setOut(new PrintStream(new OutputStream() {
                public void write(int b) {
                    //DO NOTHING
                }
            }));
    System.out.println("this should go to /dev/null, but it doesn't because it's not supported on other platforms");

    System.setOut(original);
    System.out.println("this should go to stdout");
}
catch (Exception e) {
    e.printStackTrace();
}

【讨论】:

  • 在windows上有一个替代的NUL设备; IIRC 它是“NUL:”作为“文件名”。
  • 哇哦,这真是我第一次听说!要编辑帖子。谢谢!
  • 但是 +1,因为这是正确的独立于平台的答案。但是,为了提高效率,我也会覆盖 write(byte[],int,int)。
  • 我会指出,在大多数情况下,您可能应该在 finally 块中恢复原始 OutputStream。
  • Apache Commons IO 中有新的 NullOutputStream。
【解决方案2】:

您可以将下面的类 NullPrintStream 用作:

PrintStream original = System.out;
System.setOut(new NullPrintStream());
System.out.println("Message not shown.");
System.setOut(original);

NullPrintStream 类是...

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class NullPrintStream extends PrintStream {

  public NullPrintStream() {
    super(new NullByteArrayOutputStream());
  }

  private static class NullByteArrayOutputStream extends ByteArrayOutputStream {

    @Override
    public void write(int b) {
      // do nothing
    }

    @Override
    public void write(byte[] b, int off, int len) {
      // do nothing
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
      // do nothing
    }

  }

}

【讨论】:

  • 在哪里可以获得 NullPrintStream?哪个图书馆?还是我需要自己创建课程?
  • 我的代码真的需要这个......它绝对是一个让 stdout 静音的救命稻草。
【解决方案3】:

从 JDK 11 开始就有 OutputStream.nullOutputStream()。它完全符合您的要求:

System.setOut(new PrintStream(OutputStream.nullOutputStream());

【讨论】:

    【解决方案4】:

    老问题,我知道,但这条小线可以在 Windows 上解决问题吗?

    System.setOut(new PrintStream(new File("NUL")));

    代码少得多,对我来说看起来很直接。

    【讨论】:

      【解决方案5】:

      我认为 System.setOut(null);也应该工作。至少它对我有用。

      【讨论】:

      • 对 System.out.anything 的任何后续调用都将导致抛出 NullPointerException。如果这对你有用,那么你一定是在某个地方发现了异常。
      • 虽然这可能是您想要做的(让 System.out 快速而嘈杂地失败),但这肯定不是 OP 所要求的,它也不适用于他的示例代码
      猜你喜欢
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      相关资源
      最近更新 更多