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

更改了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. 【android】[转]Android软件测试的日志文件抓取简介

    1    log文件分类简介 实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb s ...

  2. Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析

    转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...

  3. Hadoop专业解决方案-第1章 大数据和Hadoop生态圈

    一.前言: 非常感谢Hadoop专业解决方案群:313702010,兄弟们的大力支持,在此说一声辛苦了,经过两周的努力,已经有啦初步的成果,目前第1章 大数据和Hadoop生态圈小组已经翻译完成,在此 ...

  4. ApacheKylin笔记

    基本没有什么难度,只要照着官网上配置就行,这里有以下几点需要注意: HBase一定要用2.0以下的,切记.否则会报找不到方法的错误! 需要配置修改kylin.properties文件里的spark配置 ...

  5. Python网络爬虫-requests模块(II)

    有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的,例如: #!/usr/bin/env ...

  6. c++开发环境搭建

    >>>>>>>>>>>>>>>>>>>>>开发环境搭建<<&l ...

  7. bzoj 1288: Neighbours

    Description 很久以前, 有一个小小的国度, 为了方便, 我们可以把它想象为一个大大的矩形, 矩形的左下角为(0, 0), 右上角为(w, h), 共有(w + 1) * (h + 1)个整 ...

  8. bzoj4236 JOIJOI

    Description JOIOJI桑是JOI君的叔叔.“JOIOJI”这个名字是由“J.O.I”三个字母各两个构成的. 最近,JOIOJI桑有了一个孩子.JOIOJI桑想让自己孩子的名字和自己一样由 ...

  9. 提取数据之goose使用

    1.简介 Python-goose项目是用Python重写的Goose,Goose原来是用Java写的文章提取工具.Python-goose的目标是给定任意资讯文章或者任意文章类的网页,不仅提取出文章 ...

  10. Number常用方法函数

    Number类型应该是ECMAScript中最令人关注的数据类型了,这种类型使用IEEE754来表示整数和浮点数,并针对Number相关特点定义了一系列相关的方法函数. isFinite() 在Jav ...