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

更改了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);

}
}

注释和取消注释 程序中的log日志的更多相关文章

  1. 程序中编写log日志

    public string logFile; ; private Stream s = null; StreamWriter sw = null; /// <summary> /// 用l ...

  2. Eclipse中各种文件的注释与取消注释的快捷键

    Eclipse中各种文件的注释与取消注释的快捷键 Java文件: 注释和取消注释的快捷键都是:CTRL + / 或 Shift+Ctrl+C JS文件: 注释和取消注释的快捷键都是:CTRL + / ...

  3. IDEA XML注释与取消注释快捷键

    IntelliJ IDEA和eclipse中编辑Java文件时,注释和取消注释的快捷键都是: "CTRL + / " 编辑xml文件时, 注释:CTRL + SHIFT + / 取 ...

  4. XML注释与取消注释快捷键

    IntelliJ IDEA和eclipse中编辑Java文件时,注释和取消注释的快捷键都是: "CTRL + / " 编辑xml文件时, 注释:CTRL + SHIFT + / 取 ...

  5. matlab 大块注释和取消注释的快捷键

    matlab 大块注释和取消注释的快捷键 注释:Ctrl+R 取消注释:Ctrl +T

  6. 在C#中winform程序中应用nlog日志工具

    在C#中winform程序中应用nlog日志工具,配置文件简单应用. 文件名 nlog.config,请注意修改属性为"始终复制",发布时候容易遇到不存在文件的错误提示. 通过Nu ...

  7. 如何在xml文件中加注释或取消注释

    1)加注释:Ctrl+Shift+/ 2)取消注释:Ctrl+Shift+\ 本文欢迎转载,但请注明作者与出处: 作者:Sophia 出处:http://www.cnblogs.com/Sophia- ...

  8. Visual Studio 注释与取消注释快捷键

    注释:        先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U

  9. VS注释与取消注释快捷键

    最近在使用VS2010开发ASP.Net,突然发现想全部注释时找不到注释的快捷键,网上查了下,原来很简单,只是需要使用组合键. 注释:        先CTRL+K,然后CTRL+C 取消注释: 先C ...

随机推荐

  1. BASIC-2_蓝桥杯_01字串

    问题描述 对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能.它们的前几个是: 请按从小到大的顺序输出这32种01串. 输入格式 本试题没有输入. 输出格式 输出32行,按从小到大的顺 ...

  2. Windows下永久解决数据库乱码 utf8 转 gbk

    产生乱码原因 因为windows终端的默认字符集是gbk编码,而mysql数据库是utf8的编码,所以会产生乱码问题 解决乱码问题(临时修改) 询当前数据库默认编码: mysql> show v ...

  3. java实验四——找鞍点

    package hello; public class 实验四 { public static void main(String[] args) { // TODO Auto-generated me ...

  4. 关于java.lang.IncompatibleClassChangeError: Implementing class错误解决

    在javaagent使用asm字节码编程的时候启动异常,java.lang.IncompatibleClassChangeError: Implementing class, 这是包冲突引起的问题,加 ...

  5. MariaDB管理系统

    MariaDB管理系统 [root@c4kaichen@163 ~]# yum install mariadb[root@c4kaichen@163 ~]# yum install -y mariad ...

  6. SQL的三种连接方式内连接、左连接、外连接

    1.内连接 select * from table_a x inner join table_b y on x.a_id = y.b_id 返回两个表关键字x.a_id = y.b_id的交集数据集 ...

  7. C# AtomicInt

    using System; using System.Threading; /// <summary> /// Provides lock-free atomic read/write u ...

  8. 使用CMQ和SCF实现邮件发送

    准备腾讯云 API 调用工具 使用 API 命令行工具来管理和运行无服务器云函数(SCF),下面就先来安装配置该工具. 安装 Python 和 PIP Python 环境是腾讯云命令行工具运行时的必要 ...

  9. 代码生成器 CodeSmith 的使用(六)

    在上一篇的版本中,我们生成了数据库所有表中的字段,如果要使数据库中的单个表 生成 PetaPoco 构架下的 ORM 映射,使那怎么办.这是这篇博客的主要内容. 首先来看完整的 Camel 规则模板: ...

  10. 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 Week6 20165233

    Exp4 恶意代码分析 实验内容 一.基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. 使用wi ...