Search code examples
javaprintstream

How to change back to standard output console?


I tried to PrintStream to a text file for writing a bunch of output to a file.

But after the file is created, I was trying to switch back to the standard output just to continue on with other processes, but I have no idea about doing that switch. Seems like I have to setOut to the standard console, but how do I do that?

Below is the code that I used to output to a text file. Any ideas?

PrintStream out = new PrintStream(fistr);
System.setOut(out);

Thanks.


Solution

  • Why don't you save it before modifying it, so that you can go back?

    PrintStream standard = System.out;
    PrintStream out = new PrintStream(fistr);
    System.setOut(out);
    /*
     some other stuff
    */
    System.setOut(standard);