通过packageName递归查找包下的文件

**
 * Created with IntelliJ IDEA.
 * 通过packagename 递归查找 包下的文件
 * @Description:
 * @author: bowang
 * @create: 2020-06-26 下午
 **/
public class SearchPath {

    public static void main(String[] args) {

        SearchPath searchPath = new SearchPath();
        searchPath.scanPackage("com/bo/wang");
    }

    private String replaceTo(String packageName) {
        return packageName.replaceAll("\\.","/");
    }

    private void scanPackage(String packageName) {
        URL url = this.getClass().getClassLoader().getResource(replaceTo(packageName));
        String file = url.getFile();
        System.out.println(file);

        File f = new File(file);

        String[] list = f.list();

        for (String path : list) {
            File filePath = new File(file +"/"+path);

            if (filePath.isDirectory()) {
                scanPackage(packageName + "." + path);
            } else {
                System.out.println(packageName + "." + path);
            }
        }
    }
}

 

你可能感兴趣的:(java)