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

更改了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. Oracle RAC的机制与测试方法

    Oracle RAC的机制与测试方法 标签: rac 机制 测试 2016-05-25 09:54 1150人阅读 评论(0) 收藏 举报  分类: oracle(2)  1.RAC原理 Oracle ...

  2. SpringCloud负载均衡笔记

    SpringCloud的负载均衡组件用的是Ribbon,这个东西就是服务消费者.它只是一个配置用的中转器,放在 Zuul 和 Eureka Client 之间用来转发的,它里面写了好多规则,用来指定负 ...

  3. 《java笔记》

    1.判断是否是整数,包含正数和负数 /** * 判断是否是整数包含正负 */ public static boolean isInteger(String str){ Pattern pattern ...

  4. my sql 只展示 前10条数据的写法

    select * from 表 where 条件 limit 10 这里想看多少条 limit 后面的数字就是多少

  5. autoit 中文API

    中文API 参考地址: http://www.jb51.net/shouce/autoit/ 虫师的selelnium里面也有简单的说 环境搭建+上传弹窗的小案例

  6. Java 经典练习题_Day06

    面向对象与面向过程的比较 类与对象的关系 成员变量和局部变量 构造方法 this 构造代码块 局部代码块 匿名对象 一. 选择题 1.下列各项中关于面向对象及面向过程的说法错误的是:(BDE) A.  ...

  7. [UE4]C++ STL总结

    STL概述 STL (Standard Template Library, 标准模板库) 是惠普实验室开发的一系列软件的统称.主要核心分为三大部分:容器(container).算法(algorithm ...

  8. pandas的merge函数

    pandas.merge(left,right,how='inner',on=None,left_on=None,right_on=None,left_index=False,right_index= ...

  9. python + docker, 实现天气数据 从FTP获取以及持久化(五)-- 利用 Docker 容器化 Python 程序

    背景 不知不觉中,我们已经完成了所有的编程工作.接下来,我们需要把 Python 程序 做 容器化 (Docker)部署. 思考 考虑到项目的实际情况,“持久化天气”的功能将会是一个独立的功能模块发布 ...

  10. EMNLP 2018 | 用强化学习做神经机器翻译:中山大学&MSRA填补多项空白

    人工深度学习和神经网络已经为机器翻译带来了突破性的进展,强化学习也已经在游戏等领域取得了里程碑突破.中山大学数据科学与计算机学院和微软研究院的一项研究探索了强化学习在神经机器翻译领域的应用,相关论文已 ...