In Java, how can I redirect System.out to null then back to stdout again? -


i've tried temporarily redirect system.out /dev/null using following code doesn't work.

system.out.println("this should go stdout");  printstream original = system.out; system.setout(new printstream(new fileoutputstream("/dev/null"))); system.out.println("this should go /dev/null");  system.setout(original); system.out.println("this should go stdout"); // not getting printed!!! 

anyone have ideas?

man, not good, because java cross-platform , '/dev/null' unix specific (apparently there alternative on windows, read comments). best option create custom outputstream disable output.

try {     system.out.println("this should go stdout");      printstream original = system.out;     system.setout(new printstream(new outputstream() {                 public void write(int b) {                     //do nothing                 }             }));     system.out.println("this should go /dev/null, doesn't because it's not supported on other platforms");      system.setout(original);     system.out.println("this should go stdout"); } catch (exception e) {     e.printstacktrace(); } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -