36

I've tried to temporarily redirect System.out to /dev/null using the following code but it doesn't work.

System.out.println("this should go to stdout");

PrintStream original = System.out;
System.setOut(new PrintStream(new FileOutputStream("/dev/null")));
System.out.println("this should go to /dev/null");

System.setOut(original);
System.out.println("this should go to stdout"); // This is not getting printed!!!

Anyone have any ideas?

dgrant
  • 1,417
  • 3
  • 16
  • 23

5 Answers5

53

Man, this is not so good, because Java is cross-platform and '/dev/null' is Unix specific (apparently there is an alternative on Windows, read the comments). So your best option is to create a custom OutputStream to disable output.

try {
    System.out.println("this should go to stdout");

    PrintStream original = System.out;
    System.setOut(new PrintStream(new OutputStream() {
                public void write(int b) {
                    //DO NOTHING
                }
            }));
    System.out.println("this should go to /dev/null, but it doesn't because it's not supported on other platforms");

    System.setOut(original);
    System.out.println("this should go to stdout");
}
catch (Exception e) {
    e.printStackTrace();
}
Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
20

You can use the class NullPrintStream below as:

PrintStream original = System.out;
System.setOut(new NullPrintStream());
System.out.println("Message not shown.");
System.setOut(original);

And the class NullPrintStream is...

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class NullPrintStream extends PrintStream {

  public NullPrintStream() {
    super(new NullByteArrayOutputStream());
  }

  private static class NullByteArrayOutputStream extends ByteArrayOutputStream {

    @Override
    public void write(int b) {
      // do nothing
    }

    @Override
    public void write(byte[] b, int off, int len) {
      // do nothing
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
      // do nothing
    }

  }

}
Vladimir
  • 201
  • 2
  • 2
10

Since JDK 11 there is OutputStream.nullOutputStream(). It does exactly what you are looking for:

System.setOut(new PrintStream(OutputStream.nullOutputStream()));
Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
Qw3ry
  • 1,319
  • 15
  • 31
3

Old question, I know, but would this small line do the trick on Windows?

System.setOut(new PrintStream(new File("NUL")));

Much less code and looks pretty direct to me.

E.S.
  • 2,733
  • 6
  • 36
  • 71
-4

I think System.setOut(null); should work too. At least it worked for me.

  • 1
    Any subsequent calls to System.out.anything will cause a NullPointerException to be thrown. If that worked for you, then you must be catching the exception somewhere. – John Oct 19 '15 at 17:43
  • While this might be what you want to do (make System.out fail fast and noisy), it is certainly not what OP asks, and it does not work with his example code either – Qw3ry Sep 30 '19 at 13:26