Is there a Null OutputStream in Java?
Asked Answered
P

9

135

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?

Penicillate answered 27/3, 2009 at 23:33 Comment(0)
D
53

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.

Dicta answered 4/10, 2018 at 0:12 Comment(0)
M
128

Java 11+:

OutputStream.nullOutputStream()

Java 11-:

/** Writes to nowhere. */
public class NullOutputStream extends OutputStream {
  @Override
  public void write(int b) throws IOException {}
}
Milford answered 28/3, 2009 at 10:38 Comment(2)
This can also be easily implemented wherever you need it using something like this: OutputStream nullOutputStream = new OutputStream() { @Override public void write(int b) { } };Bron
for performance reasons you should also override the other wirte(...) methods, see comments in the java.io.OutputStream source codeNaumachia
C
71
D
53

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.

Dicta answered 4/10, 2018 at 0:12 Comment(0)
E
46

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.

Enervate answered 22/5, 2012 at 18:45 Comment(0)
M
15

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.

Minotaur answered 27/3, 2009 at 23:33 Comment(1)
C
5

Java 11 added OutputStream.nullOutputStream()

Corley answered 27/12, 2019 at 12:38 Comment(0)
G
3

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)

Garling answered 27/3, 2009 at 23:45 Comment(1)
I get the error: "cannot assign a value to final variable out"Adrienneadrift
V
2

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!

Valerievalerio answered 25/11, 2018 at 19:30 Comment(0)
M
0

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
Monarda answered 10/9, 2014 at 17:45 Comment(1)
This is Windows specific.Finial

© 2022 - 2024 — McMap. All rights reserved.