读取包路径

本文介绍了一种在Java中扫描指定包下所有类文件并将其加载到HashMap容器的方法。通过使用ClassPath和File类,文章详细展示了如何将类文件路径转换为实际的文件系统路径,并将这些路径存储在容器中以备后续使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  private static HdSpringUtils instance = null;

    //根据包路径 读取文件 : 将文件存入到容器中
    //容器可以是list或者map

    public static Map<String, String> classMap = new HashMap<>(128);

    /**
     * 扫描类文件到map容器中:
     *  根据包路径 + 文件名 ===》 get 什么
     * @param packageName
     */
    public void doScan2Map(String packageName) {
        String temp = packageName.replaceAll("\\.", "/");
        URL url = this.getClass().getClassLoader().getResource(temp);
        /**
         * 文件路径
         */
        //System.out.println(url);//
        File file = new File(url.getFile());
        for (File f : file.listFiles()) {
            if(f.isDirectory()) {
                doScan2Map(packageName + "." + f.getName());
            } else {
                if(!f.getName().endsWith(".class")) {
                    continue;
                } else {
                    String clazzName = packageName + "." + f.getName().replaceAll(".class","");
                    classMap.put(clazzName, null);
                }
            }
        }
    }
    private void doScanner(String scanPackage) {
        URL url = this.getClass().getClassLoader().getResource(scanPackage.replaceAll("\\.","/"));
        File classDir = new File(url.getFile());
        for (File file : classDir.listFiles()) {
            if(file.isDirectory()){ doScanner(scanPackage + "." +  file.getName());}else {
                if(!file.getName().endsWith(".class")){continue;}
                String clazzName = (scanPackage + "." + file.getName().replace(".class",""));
                classMap.put(clazzName,null);
            }
        }
    }
    private void doScanner2(String scanPackage) {
        URL url = this.getClass().getClassLoader().getResource(scanPackage.replaceAll("\\.","/"));
        File classDir = new File(url.getFile());
        for (File file : classDir.listFiles()) {
            if(file.isDirectory()){ doScanner(scanPackage + "." +  file.getName());}else {
                if(!file.getName().endsWith(".class")){continue;}
                String clazzName = (scanPackage + "." + file.getName().replace(".class",""));
                classMap.put(clazzName,null);
            }
        }
    }


    public static void main(String[] args) throws Exception {
        String s = "G:/my_maven_workspace_git/javaSpaceForGupao/homework/proxy/target/classes/com/hongdu";
        URL url = HdSpringUtils.class.getClassLoader().getResource("");
        System.out.println(url);
        //file:/G:/my_maven_workspace_git/javaSpaceForGupao/homework/proxy/target/classes/
        URL url1 = HdSpringUtils.class.getResource("");
        System.out.println(url1);
        //file:/G:/my_maven_workspace_git/javaSpaceForGupao/homework/proxy/target/classes/com/hongdu/gupao/spring/utils/
        URL url2 = HdSpringUtils.class.getResource("/");
        System.out.println(url2);
        //file:/G:/my_maven_workspace_git/javaSpaceForGupao/homework/proxy/target/classes/
        URL url3 = HdSpringUtils.class.getResource("/com/hongdu");
        System.out.println(url3);
        System.out.println(url3.getFile());
        File file = new File(url3.getFile());
        System.out.println("文件遍历开始---------------------------------------");
        HdJavaEveUtils.printFiles(file);
        System.out.println("文件遍历结束---------------------------------------");
        //file:/G:/my_maven_workspace_git/javaSpaceForGupao/homework/proxy/target/classes/com/hongdu
        URL url4 = HdSpringUtils.class.getResource("/com/hongdu");
        System.out.println(url4);

        HdSpringUtils instance = new HdSpringUtils();
        System.out.println("----------------------------------ClassLoader加载得到的路径开始----------");
        URL url5 = instance.getThisUrl();
        System.out.println(url5);
//        HdSpringUtils.getInstance().doScan2Map("com.hongdu");
//        HdJavaEveUtils.printMap(classMap);
    }

    /**
     *
     * @return
     */
    private  URL getThisUrl() {
        /**
         * classLoader : 相对路径 前面不能加斜杠
         */
        URL url5 = this.getClass().getClassLoader().getResource("com/hongdu" );
        return url5;
    }

    public static HdSpringUtils getInstance() {
        if(instance == null) {
            instance = new HdSpringUtils();
            return  instance;
        }
        return instance;
    }

    public HdSpringUtils() {}

    /**
     * 扫描到 list中 不太合适, 因为要用到的时候查找太慢了!
     * @param packageName
     */
    public static void doScan2List(String packageName) {

    }
    @Test
    public void test() {
        System.out.println(this.getClass().getClassLoader().getResource(""));
        String packageName = "com.hongdu";
        System.out.println(packageName.replaceAll("\\.", "/"));
        System.out.println(packageName);
        doScan2Map(packageName);
        HdJavaEveUtils.printMap(classMap);
    }
    @Test
    public void scanPackageTest() {
        System.out.println(this.getClass().getClassLoader().getResource(""));
        String packageName = "com.hongdu";
        System.out.println(packageName.replaceAll("\\.", "/"));
        System.out.println(packageName);
        doScanner(packageName);
        HdJavaEveUtils.printMap(classMap);
    }
    @Test
    public void scanPackage2Test() {
        System.out.println(this.getClass().getClassLoader().getResource(""));
        String packageName = "com.hongdu";
        System.out.println(packageName.replaceAll("\\.", "/"));
        System.out.println(packageName);
        doScanner2(packageName);
        HdJavaEveUtils.printMap(classMap);
    }

    @Test
    public void test03() {
        System.out.println(this.getClass().getClassLoader().getResource(""));
        doScan2Map("com.hongdu");
        HdJavaEveUtils.printMap(classMap);
    }

    @Test
    public void enumTest() {
        System.out.println(FileSuffixType.valueOf(FileSuffixType.SQL.getSuffix()));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值