Java中是否有空输出流?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/691813/
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-11 18:24:59  来源:igfitidea点击:

Is there a Null OutputStream in Java?

javaoutputstream

提问by Brandon Yarbrough

I need to specify an OutputStreamfor an API I'm using, but I don't actually have a need for the output. Does Java have an OutputStreamequivalent to > /dev/null?

我需要为OutputStream我正在使用的 API指定一个,但我实际上不需要输出。Java 有OutputStream等价于> /dev/null吗?

采纳答案by leventov

Since Java 11, there is a static utility that does exactly what you need, a static factory method OutputStream.nullOutputStream():

从 Java 11 开始,有一个静态实用程序可以完全满足您的需求,即静态工厂方法OutputStream.nullOutputStream()

Returns a new OutputStream which discards all bytes. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect.

返回一个丢弃所有字节的新 OutputStream。返回的流最初是打开的。通过调用 close() 方法关闭流。对 close() 的后续调用无效。

回答by Uri

Not in the standard library AFAIK, but it shouldn't be difficult to create one by overriding write in OutputStream

不在标准库AFAIK中,但通过覆盖OutputStream中的write来创建一个应该不难

回答by Jon

Java doesn't it would seem but Apache Commons IO does. Take a look at the following:

Java 似乎不是,但 Apache Commons IO 确实如此。看看以下内容:

https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/output/NullOutputStream.html

https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/output/NullOutputStream.html

Hope that helps.

希望有帮助。

回答by OscarRyz

No, but it is pretty easy to implement.

不,但它很容易实现。

See this question "How to remove System.out.println from codebase"

看到这个问题“如何从代码库中删除 System.out.println”

And then you just have to:

然后你只需要:

System.setOut( DevNull.out );

Or something like that :)

或类似的东西 :)

System.setOut(PrintStream)

System.setOut(PrintStream)

回答by Vineet Reynolds

Rehashing the answers already provided -

重新整理已经提供的答案-

Java does not have a NullOutputStreamclass. You could however roll your own OutputStreamthat ignores any data written to it - in other words write(int b), write(byte[] b)and write(byte[] b, int off, int len)will have empty method bodies. This is what the Common IO NullOutputStreamclass does.

Java 没有NullOutputStream类。然而,你可能会推出自己的OutputStream忽略写入任何数据-换句话说write(int b)write(byte[] b)write(byte[] b, int off, int len)将空的方法体。这就是 Common IONullOutputStream类所做的。

回答by McDowell

/**Writes to nowhere*/
public class NullOutputStream extends OutputStream {
  @Override
  public void write(int b) throws IOException {
  }
}

回答by haylem

It's not mentioned yet, so I'll also add Guava's ByteStreams.nullOutputStream(), as some might prefer Guava over Apache Commons IO or already have it in their project.

还没有提到,所以我也会添加GuavaByteStreamsnullOutputStream(),因为有些人可能更喜欢 Guava 而不是 Apache Commons IO,或者已经在他们的项目中使用了它。

Note:If you use an older version of Guava (from 1.0 to 13.0), you want to use com.google.io.NullOutputStream.

注意:如果您使用旧版本的 Guava(从 1.0 到 13.0),您需要使用com.google.io.NullOutputStream

回答by gostone

ByteArrayOutputStreamis what you want (assuming that the API will be outputting text). Just instantiate a new one.

ByteArrayOutputStream是您想要的(假设 API 将输出文本)。只需实例化一个新的。

回答by Dominik

I believe that this is what you're looking for, I was looking for the same thing: This is for redirecting output streams from standard error, standard out in ProcessBuilder objects.

我相信这就是你要找的东西,我在找同样的东西:这是为了从标准错误重定向输出流,在 ProcessBuilder 对象中标准输出。

Blockquote

块引用

pb.redirectError( ProcessBuilder.Redirect.appendTo( new File( "NUL:" ) ) );
  • Dom
  • 多姆

回答by Red Boy

There is new boy in the town, that takes care of this like a charm, just few lines of code should work. Its JDK 11and nullWriter()has been introduced there, that takes care of this. Here goes the code to deal with same old problem, new way without worrying about Operating System(OS).

镇上有一个新男孩,他像魅力一样照顾这个,只需几行代码就可以工作。它的JDK 11nullWriter()已经在那里引入,可以解决这个问题。这是处理相同旧问题的代码,无需担心操作系统(OS)的新方法。

String fileContent = "Welcome to StackOverflow readers !! Here goes the question link...";
Writer writer = Writer.nullWriter();
writer.write(fileContent);
writer.close();

Hope this may help someone!

希望这可以帮助某人!