注释和取消注释 程序中的log日志

时间:2021-08-22 14:13:04

有点简单,但也是原创哦。。亲测有效,期待指正。

更改了log多行的问题。。

例如//Log

Util;

一、注释log
    import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class CommentLog {

public static String dirpath = "/home/google/com";
    // public static String dirpath = "/home/java";
    public static String filepath = "/home/java/test1";

public static String newStr = "LogUtil.";
    public static String oldStr = "//LogUtil.";
    public static boolean judge = false;
    public static String str = null;

public static boolean MyPanduan(String str) {
      String strTest = str.trim();
      if(!strTest.endsWith(";")) {
            judge = true;
        } else {
            judge = false;
        }
        return judge;
    }

public static void writeStrtoFile(StringBuffer sb1, String path) {

try {
            String sb = "" + sb1;
            FileOutputStream fos = new FileOutputStream(path, false);// true原有续写,false是追加。如果源文件不存在就新建了
            fos.write(sb.getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public static void replaceDir(String Mdirpath) {
        File file = new File(Mdirpath);
        if (file.isDirectory()) {
            File[] tempList = file.listFiles();
            for (File fi : tempList) {
                if (fi.isFile()) {
                    if (!fi.getAbsoluteFile().equals("LogUtil.java")) {
                        replaceFile(fi.getAbsolutePath());
                    }
                    judge = false;
                } else {
                    replaceDir(fi.getAbsolutePath());
                }
            }
        }
    }

public static void replaceFile(String path) {
        try {
            // read file content from file
            StringBuffer sb = new StringBuffer("");
            FileReader reader = new FileReader(path);

BufferedReader br = new BufferedReader(reader);
            while ((str = br.readLine()) != null) {

if (str.contains(newStr)) {
                    str = str.replaceAll(newStr, oldStr);
                    MyPanduan(str);
                    sb.append(str);
                } else {
                    if (judge == true) {
                        str = "//".concat(str);
                        MyPanduan(str);
                    }
                    sb.append(str);
                }
                sb.append("\n");
            }

br.close();
            reader.close();
            if (sb != null) {
                writeStrtoFile(sb, path);
            }
        }

catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

public static void main(String[] args) {
        replaceDir(dirpath);

}
}

二、取消注释

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CancelCommentLog {

public static String dirpath = "/home/google/com";
//public static String dirpath = "/home/java";
public static String filepath = "/home/java/test1";

public static String newStr = "LogUtil";
    public static String oldStr = "//LogUtil";
    public static boolean judge = false;
       public static String str = null;

public static boolean MyPanduan(String str) {
        str = str.trim();
        if(!str.endsWith(";")){
            judge = true;
        } else {
            judge = false;
        }
        return judge;
    }

public static void writeStrtoFile(StringBuffer sb1, String path) {

try {
            String sb = ""+sb1;
            FileOutputStream fos = new FileOutputStream(path, false);// true原有续写,false是追加。如果源文件不存在就新建了
            fos.write(sb.getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void replaceDir(String Mdirpath){
          File file = new File(Mdirpath);
          if (file.isDirectory()) {
              File[] tempList = file.listFiles();
              for (File fi : tempList) {
                  if (fi.isFile()) {
                      replaceFile(fi.getAbsolutePath());
                      judge = false;
                  }
                  else{
                      replaceDir(fi.getAbsolutePath());
                  }
              }
          }
    }
    
    
    public static void replaceFile(String path){
        try {
            // read file content from file
            StringBuffer sb= new StringBuffer("");
            FileReader reader = new FileReader(path);

BufferedReader br = new BufferedReader(reader);
            while((str = br.readLine()) != null) {
                
                String regEx=" +//LogUtil";  
                String regEx1="//LogUtil";

Pattern pat=Pattern.compile(regEx);  
                Pattern pat1=Pattern.compile(regEx1);

Matcher mat=pat.matcher(str);  
                Matcher mat1=pat1.matcher(str);

if(mat.find()){
                   str=mat.replaceAll(newStr);
                    MyPanduan(str);
                     sb.append(str);
                  }
                else if(mat1.find()){
                      str=mat1.replaceAll(newStr);
                      MyPanduan(str);
                       sb.append(str);
                }else{
                
                    if(judge == true){
                      str =str.replaceAll("//", " ");
                       MyPanduan(str);
                    }
                    sb.append(str);
                }
                sb.append("\n");
            }

br.close();
            reader.close();
            if(sb!=null){
               writeStrtoFile(sb,path);
            }
      }

catch(FileNotFoundException e) {
                  e.printStackTrace();
            }
            catch(IOException e) {
                  e.printStackTrace();
            }
    }
    public static void main(String[] args) {
        replaceDir(dirpath);

}
}