java-软著获取项目源代码脚本

去除空格 和注释

正则表达式:

去除空格:^\s*\n
去除注释:/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/|[ \t]*//.*

脚本

将需要的项目源码放入到对应的文件

public class ReadUtil {
    public static String baseUrl = "D:\\idea\\project\\cptc\\cptc-capacity-center\\src\\main\\java";

    public static void main(String[] args){
        methodName(baseUrl);
    }

    //读取某个路径下的文件名
    public static void methodName(String filePath){
        //获取文件路径所指向的文件
        File file = new File(filePath);
        System.out.println(filePath);
        //获取文件夹下所有的文件
        File[] files = file.listFiles();
        //遍历获取到的文件,如果是文件夹则继续向下获取
        for (File file1 : files) {
            String url = filePath;
            //如果是文件夹,url往下增加一级
            if (file1.isDirectory()) {
                url += "\\"+file1.getName();
                //继续获取文件
                methodName(url);
            }else {
                String name1 = file1.getName();
                String content = readFileContent(filePath+"\\"+name1);
                System.out.println(name1);
                inputFile(content);
            }

        }
        System.out.println("结束!");
    }


    //处理str字段
    public static String handleStrWins(String str){
        str = str.substring(0, str.indexOf(".")).replace(" ","")
                .replace("、","")
                .replace("/","");
        return str;
    }

    /* @Title: readFileContent
     * @Description: 读取文件内容
     * @param filePath
     * @return
     */
    public static String readFileContent(String filePath) {
        StringBuilder result = new StringBuilder();
        try {
//          BufferedReader bfr = new BufferedReader(new FileReader(new File(filePath)));
            BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = bfr.readLine()) != null) {
                if (lineTxt.equals("") || lineTxt.equals(" "))
                    continue;
                if (filePath.contains("html")){
                    lineTxt.replaceAll(" ","");//去除字符串中的空格,回车,换行符,制表符
                    System.out.println(lineTxt);
                }
                result.append(lineTxt).append("\n");
            }
            bfr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 把获取的字符串进行写入
     * @param str
     */
    public static void inputFile(String str){
        FileWriter writer;
        try {
            //写到对应的文件下,这个文件就是取消空格合并代码生成的一个总文件
            writer = new FileWriter("E:/ruanzhu.txt",true);
            writer.write(str);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
 }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值