System.out.println removal/comment from multiple java source files

后端 未结 7 1887
失恋的感觉
失恋的感觉 2021-01-13 11:08

I want to remove/comment all the occurrences of System.out.println from my java code. This System.out.println may be inside if ,

7条回答
  •  臣服心动
    2021-01-13 11:56

    You can make all System.out.println in your application not to print anything in console. Create a class like:

    import java.io.PrintStream;
    
    
    public class MyStream extends PrintStream {
       private static final MyStream INSTANCE = new MyStream();
    
       public static void init() {
          System.setOut(INSTANCE);
       }
    
       private MyStream() {
          super(System.out);
       }
    
       @Override
       public void println(Object x) {
          return;
       }
    
       @Override
       public void println(String x) {
          return;
       }
    }
    

    Use it as:

    public class Test {
    
        public static void main(String... args) {
            MyStream.init();
            System.out.println("This line will not print");
        }
    }
    

    The will not be printed anymore.

提交回复
热议问题