Skip to content
Advertisement

Is there a Null OutputStream in Java?

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement