Writing to the real STDOUT after System.setOut

后端 未结 4 579
栀梦
栀梦 2020-12-24 06:43

I\'m trying to intercept System.out and System.err, but maintain the ability to write to the original streams directly when necessary.

PrintStream ps = Syste         


        
4条回答
  •  孤独总比滥情好
    2020-12-24 07:14

    Try something like this :

      PrintStream original = new PrintStream(System.out);
    
      // replace the System.out, here I redirect to NUL
      System.setOut(new PrintStream(new FileOutputStream("NUL:")));
      System.out.println("bar");  // no output
    
      // the original stream is still available 
      original.println("foo");  // output to stdout
    

提交回复
热议问题