软著获取java 源代码文件,输出txt

项目场景:

项目场景:软著获取java 源代码文件,输出txt


package com.cdrundle;

import java.io.*;

/5用递归的方式得到所有工作空间中的.java文件,并输出service 实现类/
public class Lx5 {
public static void main(String[] args) throws IOException {
//这里写你需要的文件夹路径
File f = new File(“F:\cdc\test”);
File target = new File(“F:\cdc\test_service.txt”);

    BufferedWriter bw = new BufferedWriter(new FileWriter(target));
    StringBuilder sb = new StringBuilder();
    printPathAndLength(f, sb);
    write(sb.toString(), bw);
    System.out.println("结束大小:" + sb.length());
}

public static void printPathAndLength(File f, StringBuilder sb) throws IOException {

    if (f.isFile()) {
        if (f.getName().endsWith(".java") && !(f.getName().matches("(.*)Mapper(.*)"))) {
            if (f.getName().matches("(.*)ServiceImpl(.*)")) {
                if (f.length() != 0) {
                    sb.append(readFileToString(f));
                }
            }

        }
    } else {
        File[] arr = f.listFiles();
        if (arr.length > 0) {
            for (File f1 : arr) {

                if (f1.getName().equals("文件夹A")) {
                    continue;
                }
                if (f1.getName().equals("文件夹B")) {
                    continue;
                }
                if (f1.getName().equals("文件夹C")) {
                    continue;
                }
                if (f1.getName().equals("文件夹D")) {
                    continue;
                }
                if (f1.getName().equals("文件夹E")) {
                    continue;
                }

                printPathAndLength(f1, sb);
            }
        }
    }
}

private static void write(String str, Writer writer) {

    try {
        writer.write(str);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (Exception e2) {
            e2.printStackTrace();
        }

    }

}

private static String readFileToString(File file) {
    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();
    try {
        br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            String s = line.trim();
            if (s.length() == 0) {
                continue;
            }
            if (s.startsWith("/") || s.startsWith("*")) {
                continue;
            }
            sb.append(line).append("\n");
        }

    } catch (Exception e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null) {

                br.close();

            }

        } catch (Exception e2) {

            e2.printStackTrace();

        }

    }

    return sb.toString();

}

}

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值