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

更改了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版本和API Level对应关系

    API Level Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 18 JELLY_BEAN_MR2 Platform Hig ...

  2. 什么是JavaBean、bean? 什么是POJO、PO、DTO、VO、BO ? 什么是EJB、EntityBean?

    什么是JavaBean.bean?  什么是POJO.PO.DTO.VO.BO ?  什么是EJB.EntityBean?   前言:   在Java开发中经常遇到这些概念问题,有的可能理解混淆,有的 ...

  3. android 布局入门

    一.LinearLayout RelativeLayout 这俩的区别详见这里 http://www.cnblogs.com/duanweishi/p/4244233.html 二.android:l ...

  4. Hyperic Sigar API 举例

    Hyperic HQ 是什么? Hyperic HQ 是一个开源的(General Public License,GPL授权)IT资源管理框架,让用户使用统一的界面来管理各种不同的IT资源的管理,Hy ...

  5. ThinkJava-容器深入研究

    第17章 容器深入研究 17.2 填充容器 package com.cy.container; import java.util.*; class StringAddress { private St ...

  6. windows任务计划程序 坑

  7. 超链接中 utm_source, utm_medium 等参数的含义是什么?

    作者:张溪梦 Simon链接:https://www.zhihu.com/question/48724061/answer/122730629来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  8. php file_exists无效解决办法

    一:is_file 和 file_exists 的区别:当文件存在时:is_file 比 file_exists快了N倍当文件不存在时:is_file 比 file_exists慢总之一句话:file ...

  9. PHP 序列化变量的 4 种方法

    摘自: PHP 序列化变量的 4 种方法 http://www.iteye.com/news/25668

  10. HTC Vive前置摄像头API(未测试)

    /*WebCamTexture:网络摄像头材质 WebCamTexture.Play() 播放: WebCamTexture.Pause() 暂停: WebCamTexture.Stop() 停止:* ...