遍历一个指定的目录下的所有文件及该文件所对应的指定目录下的对应层数,可指定遍历的层数

3 篇文章 0 订阅
2 篇文章 0 订阅

1.采用递归进行统计,确定需求不会有很多层的情况下使用,可以维护一个层数控制放入,控制遍历的次数(在不确定目录的深度的情况下)

 /**
     * 获取目录下所有的文件路径和对应文件的层数
     *
     * @param homePath
     * @return
     */
    public List<Map<String, Integer>> mapFiles(String homePath) {

        List<Map<String, Integer>> savePathAndFile = new ArrayList<>();
        File homeDir = new File(homePath);
        if (homeDir.exists()) {
            File[] files = homeDir.listFiles();
            for (File file : files) {
                if (file.exists()) {
                    if (file.isDirectory()) {
                        String containsChildDir = file.getAbsolutePath();
                        List<Map<String, Integer>> maps = mapFiles(containsChildDir);

                        maps = maps.stream().map(si -> {
                            for (String key : si.keySet()) {
                                Integer value = si.get(key);
                                si.put(key, ++value);//使用++value,维护文件的深度记录
                            }
                            return si;
                        }).collect(Collectors.toList());
                        savePathAndFile.addAll(maps);
                    } else {
                        Map<String, Integer> pathAndFile = new HashMap<>();
                        pathAndFile.put(file.getAbsolutePath(), 1);
                        savePathAndFile.add(pathAndFile);
                    }
                }
            }
        }
        return savePathAndFile;
    }

增加一个deep版本

/**
     * 获取目录下所有的文件路径和对应文件的层数,控制查找的层数
     *
     * @param homePath
     * @return
     */
    public static List<Map<String, Integer>> mapFiles(String homePath,Integer deep) {
        List<Map<String, Integer>> savePathAndFile = new ArrayList<>();
        File homeDir = new File(homePath);
        if (homeDir.exists()) {
            File[] files = homeDir.listFiles();
            for (File file : files) {
                if (file.exists()) {
                    if (file.isDirectory()) {
                        String containsChildDir = file.getAbsolutePath();
                        List<Map<String, Integer>> maps = mapFiles(containsChildDir,deep);

                        maps = maps.stream().map(si -> {
                            for (String key : si.keySet()) {
                                Integer value = si.get(key);
                                si.put(key, ++value);
                            }
                            return si;
                        }).collect(Collectors.toList());

                        if(maps.get(0).entrySet().iterator().next().getValue()>deep){
                            return savePathAndFile;
                        }

                        savePathAndFile.addAll(maps);
                    } else {
                        Map<String, Integer> pathAndFile = new HashMap<>();
                        pathAndFile.put(file.getAbsolutePath(), 1);
                        savePathAndFile.add(pathAndFile);
                    }
                }
            }
        }
        return savePathAndFile;
    }

改造上述的代码,不采用递归

/**
     * 获取目录下所有的文件路径和对应文件的层数,控制查找的层数,非递归
     *
     * @param homePath
     * @return
     */
    public static List<Map<String,Integer>> mapFiles(String homePath) {
        List<Map<String,Integer>> savePathAndFile = new ArrayList<>();
        LinkedList<String> queue= new LinkedList<>();
        File homeDir = new File(homePath);
        if(homeDir.exists()){
            queue.add(homePath);
        }
        int deep=1;
        while (!queue.isEmpty()){
            String path = stack.pollLast();
            File file = new File(path);
            if(file.isDirectory()){
                for (File f:file.listFiles()) {
                    queue.add(f.getAbsolutePath());
                }
                ++deep;
            }else{
                Map<String, Integer> pathAndFile = new HashMap<>();
                pathAndFile.put(file.getAbsolutePath(), deep);
                savePathAndFile.add(pathAndFile);
            }
        }

        return savePathAndFile;
    }

增加层数控制

 /**
     * 获取目录下所有的文件路径和对应文件的层数,控制查找的层数,非递归
     *
     * @param homePath
     * @return
     */
    public static List<Map<String,Integer>> mapFiles(String homePath,int controlDeep) {
        List<Map<String,Integer>> savePathAndFile = new ArrayList<>();
        LinkedList<String> queue= new LinkedList<>();
        File homeDir = new File(homePath);
        if(homeDir.exists()){
            queue.add(homePath);
        }
        int deep=1;
        while (!queue.isEmpty()&&deep<=controlDeep){
            String path = stack.pollLast();
            File file = new File(path);
            if(file.isDirectory()){
                for (File f:file.listFiles()) {
                    queue.add(f.getAbsolutePath());
                }
                ++deep;
            }else{
                Map<String, Integer> pathAndFile = new HashMap<>();
                pathAndFile.put(file.getAbsolutePath(), deep);
                savePathAndFile.add(pathAndFile);
            }
        }

        return savePathAndFile;
    }

放弃递归调用有效防止栈溢出,推荐采用此类操作。生产中采用递归有可能会引发事故,因此推荐非递归操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值