场景

  • 页面上传jar包
  • 后台解压jar包
  • 页面展示所有package
  • 选择一个package
  • 页面显示class和子package
    • 选择class,进入class解析页面
    • 选择package,显示class和子package

文件夹递归选择器

package com.wuxiongwei.java.jar2;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件夹递归选择器 <br>
 * @author
 * @version 1.0.0
 */
public class FolderRecursive {
    public static void main(String[] args) {
        String path = "/Users/mac/Documents/other/bw2/test/";
        List<String> resultFileName = new ArrayList<>();

//        recursiveFile(new File(path),resultFileName);

        recursiveDic(new File(path),resultFileName);
        //获取所有文件夹
        resultFileName.forEach(name -> {
            name = name.replace(path,"");
            name = name.replace("/",".");
            //文件夹格式转package
            System.out.println(name);
        });

        //选择一个package
        String temp="com.baiwang.bop.client";
        temp = temp.replace(".","/");
        //获取指定路径下所有文件和文件夹
        getFilesByPath(new File(path+temp),resultFileName);
        resultFileName.forEach(name -> {
            System.out.println(name);
        });
    }

    //递归文件夹下所有文件和文件夹
    private static List<String> recursiveFile(File file, List<String> resultFileName){
        File[] files = file.listFiles();
        if(files==null)return resultFileName;// 判断目录下是不是空的
        for (File f : files) {
            if(f.isDirectory()){// 判断是否文件夹
                resultFileName.add(f.getPath());
                recursiveFile(f,resultFileName);// 调用自身,查找子目录
            }else
                resultFileName.add(f.getPath());
        }
        return resultFileName;
    }

    //递归文件夹下所有文件夹
    private static List<String> recursiveDic(File file, List<String> resultFileName){
        File[] files = file.listFiles();
        if(files==null)return resultFileName;// 判断目录下是不是空的
        for (File f : files) {
            if(f.isDirectory()){// 判断是否文件夹
                resultFileName.add(f.getPath());
                recursiveDic(f,resultFileName);// 调用自身,查找子目录
            }
        }
        return resultFileName;
    }

    //获取指定路径下所有文件和文件夹
    public static List<String> getFilesByPath(File file, List<String> resultFileName){
        File[] files = file.listFiles();
        if(files==null)return null;
        for (File f : files) {
            resultFileName.add(f.getPath());
        }
        return resultFileName;
    }
}