java根据所给的根目录获取底下所有文件夹结构

所写工具类背景:项目经理叫我写个工具类实现:给个项目的根目录分析java文件及jsp文件。记录文件类型、路径、文件名和包名。

定义的实体类(这里我用了easypoi以后方便写入excel文档)

@Data
public class ReadExcelDataEntity implements Serializable {

    @Excel(name = "文件", orderNum = "1")
    private String fileType;

    @Excel(name = "名称", orderNum = "2")
    private String fileName;

    @Excel(name = "路径", orderNum = "3")
    private String filePath;

    @Excel(name = "包名", orderNum = "4")
    private String packageName;
}

工具类:

public class FindAllFile {
    private static List<String> allList = new ArrayList<>();

    /**
     * 根据根目录获取所有路径信息
     *
     * @param f File
     * @return allList
     */
    public static List<String> getFile(File f) {
        File[] fList = f.listFiles();
        for (int i = 0; i < fList.length; i++) {
            if (fList[i].isDirectory() == true) {
                getFile(fList[i]);
            } else {
                allList.add(fList[i].getAbsolutePath());
            }
        }
        return allList;
    }

    /**
     * 根据类型获取class一览
     *
     * @param f        File
     * @param fileType 文件类型
     * @return
     */
    public static List<ReadExcelDataEntity> findAllFileByFileType(File f, String fileType) {
        //清空静态allList多次调用影响数据
        allList.clear();
        List<ReadExcelDataEntity> list = new ArrayList<>();
        String rootPath = f.getPath();
        String root = rootPath.replace("\", "\\");
        getFile(f);
        allList.stream()
                .forEach(x -> {
                    if (x.endsWith(fileType)) {
                        ReadExcelDataEntity entity = new ReadExcelDataEntity();
                        //获取文件类型
                        String[] split = x.split("\.");
                        String type = split[split.length - 1];
                        File file = new File(x);
                        //获取文件名称
                        String name = file.getName();
                        //获取文件路径
                        String filePath = x.replaceAll(root, " ");
                        entity.setFileType(type);
                        entity.setFilePath(filePath.trim());
                        entity.setFileName(name);
                        FileReader fr = null;
                        BufferedReader br = null;
                        if ("java".equals(fileType)) {
                            //获取文件类型为java的包名
                            try {
                                fr = new FileReader(file);
                                br = new BufferedReader(fr);
                                String line = "";
                                while ((line = br.readLine()) != null) {
                                    if (line.indexOf("package") != -1) {
                                        line = line.replaceAll("package", " ");
                                        entity.setPackageName(line.trim());
                                        list.add(entity);
                                        return;
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                try {
                                    fr.close();
                                    br.close();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        list.add(entity);
                    }
                });
        return list;
    }

    /**
     * 去除前后字符
     *
     * @param args   传入的字符串
     * @param beTrim 去除格式
     * @return
     */
    public static String trim(String args, char beTrim) {
        int st = 0;
        int len = args.length();
        char[] val = args.toCharArray();
        char sbeTrim = beTrim;
        while ((st < len) && (val[st] <= sbeTrim)) {
            st++;
        }
        while ((st < len) && (val[len - 1] <= sbeTrim)) {
            len--;
        }
        return ((st > 0) || (len < args.length())) ? args.substring(st, len) : args;
    }
}

核心代码:

public static List<String> getFile(File f) {
        File[] fList = f.listFiles();
        for (int i = 0; i < fList.length; i++) {
            if (fList[i].isDirectory() == true) {
                getFile(fList[i]);
            } else {
                allList.add(fList[i].getAbsolutePath());
            }
        }
        return allList;
    }

测试类:

public class Test3 {
    public static void main(String[] args) {
        File file = new File("E:\日本\branches\20190104\skyreservesatellite");
        List<ReadExcelDataEntity> java = FindAllFile.findAllFileByFileType(file, "jsp");
        System.out.println(java);
        System.out.println(java.size());

    }
}

测试结果:

jsp文件:

[ReadExcelDataEntity(fileType=jsp, fileName=addflightinfo.jsp, filePath=skyreservesatellitesatelliteaddflightinfo.jsp, packageName=null)]

java文件:

[ReadExcelDataEntity(fileType=java, fileName=UtilCheckerSso.java, filePath=skyreservesatellitesatellitesrcskyagencycheckerUtilCheckerSso.java, packageName=sky.agency.checker;)]
原文地址:https://www.cnblogs.com/chiang28/p/10303306.html