Resetting Standard output Stream

后端 未结 2 996
死守一世寂寞
死守一世寂寞 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条回答
  •  Happy的楠姐
    2020-12-05 02:37

    This is an old question, but it turns up in Google search all the time and I wanted to correct it. You can actually get it, by using the FileDescriptor class. Calling new PrintStream(new FileOutputStream(FileDescriptor.out))) should give you something which prints to stdout.

    import java.io.FileDescriptor;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    
    public static void main(String [] args) {
        System.err.println("error.");
        System.out.println("out.");
        System.setOut(System.err);
        System.out.println("error?");
        System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
        System.out.println("out?");
    }
    

提交回复
热议问题