批量删除代码注释

最近项目交接,公司业务被被抢了!恼火!!而且还是使用及其下流的手段抢了业务,虽然我不是老板,但是我也是公司一员,删了全部注释,让你们接!!!

此代码并不完善,不能排除一些特殊情况。删了不该删的代码,手工补回来就好了。

public class test {


    private static int count = 0;


    /**
     * 删除文件中的各种注释,包含//、/* * /等
     * @param charset 文件编码
     * @param file 文件
     */
    public static void clearComment(File file, String charset) {
        try {
            //递归处理文件夹
            if (!file.exists()) {
                return;
            }


            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File f : files) {
                    clearComment(f, charset); //递归调用
                }
                return;
            } else if (!file.getName().endsWith(".java")) {
                //非java文件直接返回
                return;
            }
            System.out.println("-----开始处理文件:" + file.getAbsolutePath());


            //根据对应的编码格式读取
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
            StringBuffer content = new StringBuffer();
            String tmp = null;
            while ((tmp = reader.readLine()) != null) {
                content.append(tmp);
                content.append("\n");
            }
            String target = content.toString();
            //String s = target.replaceAll("\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*\\/", ""); //本段正则摘自网上,有一种情况无法满足(/* ...**/),略作修改
            String s = target.replaceAll("\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*+\\/", "");
            //System.out.println(s);
            //使用对应的编码格式输出
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
            out.write(s);
            out.flush();
            out.close();
            count++;
            System.out.println("-----文件处理完成---" + count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void clearComment(String filePath, String charset) {
        clearComment(new File(filePath), charset);
    }


    public static void clearComment(String filePath) {
        clearComment(new File(filePath), "UTF-8");
    }


    public static void clearComment(File file) {
        clearComment(file, "UTF-8");
    }


    public static void main(String[] args) {
        clearComment("D:\\gzlhxc\\lhxc_xg\\src"); //删除目录下所有java文件注释
        //删除某个具体文件的注释
        //clearComment("D:\\proj\\scm\\action\\AbcdefgAction.java");
    }


}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值