Hiding System.out.print calls of a class

前端 未结 5 1569
别那么骄傲
别那么骄傲 2021-02-02 13:16

I\'m using a java library (jar file). The author of the file put in a bunch of System.out.print and System.out.printlns. Is there any way to hide these

5条回答
  •  無奈伤痛
    2021-02-02 13:26

    Change original PrintStream with a Dummy one which does nothing on it's write() method.

    Don't forget to replace original PrintStream when you finished.

    System.out.println("NOW YOU CAN SEE ME");
    
    PrintStream originalStream = System.out;
    
    PrintStream dummyStream = new PrintStream(new OutputStream(){
        public void write(int b) {
            // NO-OP
        }
    });
    
    System.setOut(dummyStream);
    System.out.println("NOW YOU CAN NOT");
    
    System.setOut(originalStream);
    System.out.println("NOW YOU CAN SEE ME AGAIN");
    

提交回复
热议问题