Is there a Null OutputStream in Java?

若如初见. 提交于 2019-11-27 01:43:19

问题


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


回答1:


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

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

Hope that helps.




回答2:


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



回答3:


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.

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




回答4:


Rehashing the answers already provided -

Java does not have a NullOutputStream class. You could however roll your own OutputStream that 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 NullOutputStream class does.




回答5:


Since Java 11, there is a static utility that does exactly what you need, a static factory method 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.




回答6:


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




回答7:


No, but it is pretty easy to implement.

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

And then you just have to:

System.setOut( DevNull.out );

Or something like that :)

System.setOut(PrintStream)




回答8:


There is new boy in the town, that takes care of this like a charm, just few lines of code should work. Its JDK 11 and 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).

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!




回答9:


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.

Blockquote

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



回答10:


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



来源:https://stackoverflow.com/questions/691813/is-there-a-null-outputstream-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!