Resetting Standard output Stream

后端 未结 2 995
死守一世寂寞
死守一世寂寞 2020-12-05 01:59

I know that there is a function in Java to set Standard Output Stream to any user defined value using System.setOut method..

But is there any method to

2条回答
  •  离开以前
    2020-12-05 02:12

    You can get hold of the file descriptor for standard out through FileDescriptor.out. To reset standard out to print to console, you do

    System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
    

    Another way is to simply hold on to the original object, as follows:

    PrintStream stdout = System.out;
    System.setOut(new PrintStream(logFile));
    
    // ...
    
    System.setOut(stdout);                   // reset to standard output
    

提交回复
热议问题