有人可以向我解释为什么只使用System.setOut(System.out);
无法将输出重置回System.out?
如果我使用:
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream originalOutput = System.out;
System.setOut(new PrintStream(output));
////
System.setOut(originalOut);
它的工作原理。 但是如果我只是用
System.setOut(System.out);
然后没有。
谢谢
Because System.setOut() changes System.out itself. In this tutorial, for example, https://www.tutorialspoint.com/java/lang/system_setout.htm there is this code:
如果未更改System.out,则该代码无法在文本文件中输出。 由于System.out更改为ByteArrayOutputStream,因此执行System.setOut(System.out)不会执行任何操作,因此基本上是System.out = System.out,此外,如本教程中所述,以便实际检查是否存在安全管理器,不允许更改输出位置。