tags:

views:

970

answers:

6

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 ?

A: 

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

Uri
+9  A: 

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

http://commons.apache.org/io/api-1.4/org/apache/commons/io/output/NullOutputStream.html

Hope that helps.

Jon
+1  A: 

There is an open implementation here. You can check the javadoc here

Marcelo Morales
+1  A: 

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.out = DevNull.out;

Or something like that :)

OscarRyz
+6  A: 

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.

Vineet Reynolds
+4  A: 
/**Writes to nowhere*/
public class NullOutputStream extends OutputStream {
  @Override
  public void write(int b) throws IOException {
  }
}
McDowell