次のコードを使用してSystem.outを/ dev/nullに一時的にリダイレクトしようとしましたが、動作しません。Javaでは、どうすればSystem.outをnullにしてからstdoutに戻すことができますか?
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!!!
誰でもアイデアはありますか?
システムで両方の行が正常に表示されています。私はJava 6 update 22を使用しています。 –
BTW:このようにSystem.outを操作することには注意してください。 –