java 递归读取前10个匹配的文件所在的全路径

需求

有个需求:在连接ftp成功后,读取指定目录下匹配正则表达式的前10个文件。很显然这个需要使用递归,因为会有不同层级的文件目录,不能写死来处理。

第一版代码展示

java 复制代码
public static void test(ChannelSftp channelSftp, 			String path, String fileNamePattern,List<String> fileList, String dirPattern) {
   // 如果已经找到了10个文件,直接返回,不再递归
   if (fileList.size() >= 10) {
       log.info("已读取10个文件,不再读取目录:{}下的文件", path);
       return;
   }
   try {
       List<Pattern> dirPatterns = new ArrayList<>();
       if (StringUtils.isNotEmpty(dirPattern)) {
           for (String pat : dirPattern.split(",")) {
               dirPatterns.add(Pattern.compile(pat.trim()));
           }
       }
       Pattern fileNamePat = Pattern.compile(fileNamePattern);

       if (isDirectory(channelSftp, path)) {
           Vector<?> vector = channelSftp.ls(path);
           for (Object o : vector) {
               ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) o;
               String fileName = file.getFilename();
               boolean isDirMatch = dirPatterns.isEmpty();
               for (Pattern dirPat : dirPatterns) {
                   if (dirPat.matcher(fileName).matches()) {
                       isDirMatch = true;
                       break;
                   }
               }
               if (fileName.equals(".") || fileName.equals("..")) {
                   continue;
               }
               if (isDirMatch || fileNamePat.matcher(fileName).matches()) {
                   String fullPath = path + fileName + (file.getAttrs().isDir() ? "/" : "");
                   manyDirFileListFor10(channelSftp, fullPath, fileNamePattern, fileList, dirPattern);
               }
           }
       } else {
           String fileName = path.substring(path.lastIndexOf("/") + 1);
           if (fileNamePat.matcher(fileName).matches()) {
               fileList.add(path);
           }
       }
   } catch (SftpException e) {
       log.error("获取FTP指定目录下的文件异常,路径:{},异常信息:{}", path, e.getMessage());
   } catch (Exception e) {
       log.error("递归获取SFTP指定目录下的文件列表失败,路径:{},异常信息:{}", path, e.getMessage());
   }
}

当fileList有10个元素了之后,走到return之后并没有结束,而是会继续往下走递归,于是有了第二版:

java 复制代码
public static void manyDirFileListFor10(ChannelSftp channelSftp, String path, String fileNamePattern,
                                       List<String> fileList, String dirPattern) {
        try {
            List<Pattern> dirPatterns = new ArrayList<>();
            if (StringUtils.isNotEmpty(dirPattern)) {
                for (String pat : dirPattern.split(",")) {
                    dirPatterns.add(Pattern.compile(pat.trim()));
                }
            }
            Pattern fileNamePat = Pattern.compile(fileNamePattern);

            if (isDirectory(channelSftp, path)) {
                Vector<?> vector = channelSftp.ls(path);
                for (Object o : vector) {
                    // 如果已经找到了10个文件,直接返回,不再递归
                    if (fileList.size() >= 10) {
                        log.info("已读取10个文件,不再读取目录:{}下的文件", path);
                        break;
                    }
                    ChannelSftp.LsEntry file = (ChannelSftp.LsEntry) o;
                    String fileName = file.getFilename();
                    boolean isDirMatch = dirPatterns.isEmpty();
                    for (Pattern dirPat : dirPatterns) {
                        if (dirPat.matcher(fileName).matches()) {
                            isDirMatch = true;
                            break;
                        }
                    }
                    if (fileName.equals(".") || fileName.equals("..")) {
                        continue;
                    }
                    if (isDirMatch || fileNamePat.matcher(fileName).matches()) {
                        String fullPath = path + fileName + (file.getAttrs().isDir() ? "/" : "");
                        manyDirFileListFor10(channelSftp, fullPath, fileNamePattern, fileList, dirPattern);
                    }
                }
            } else {
                String fileName = path.substring(path.lastIndexOf("/") + 1);
                if (fileNamePat.matcher(fileName).matches()) {
                    fileList.add(path);
                }
            }
        } catch (SftpException e) {
            log.error("获取FTP指定目录下的文件异常,路径:{},异常信息:{}", path, e.getMessage());
        } catch (Exception e) {
            log.error("递归获取SFTP指定目录下的文件列表失败,路径:{},异常信息:{}", path, e.getMessage());
        }
    }

这里将fileList.size()>=10的判断放在for循环里,这样就可以了。
----------------------------知道的越多,不知道的越多-------------------------

相关推荐
hello早上好7 分钟前
CGLIB代理核心原理
java·spring
魔镜魔镜_谁是世界上最漂亮的小仙女9 分钟前
java-web开发
java·后端·架构
爱吃小土豆豆豆豆9 分钟前
定时器和守护线程
java
Seven9729 分钟前
了解GC吗?什么是GC?
java
Edingbrugh.南空44 分钟前
Flink ClickHouse 连接器维表源码深度解析
java·clickhouse·flink
掘金-我是哪吒1 小时前
分布式微服务系统架构第157集:JavaPlus技术文档平台日更-Java多线程编程技巧
java·分布式·微服务·云原生·架构
飞翔的佩奇1 小时前
Java项目:基于SSM框架实现的忘忧小区物业管理系统【ssm+B/S架构+源码+数据库+毕业论文+开题报告】
java·数据库·mysql·vue·毕业设计·ssm框架·小区物业管理系统
RainbowSea2 小时前
跨域问题(Allow CORS)解决(3 种方法)
java·spring boot·后端
掘金-我是哪吒2 小时前
分布式微服务系统架构第155集:JavaPlus技术文档平台日更-Java线程池实现原理
java·分布式·微服务·云原生·架构
RainbowSea2 小时前
问题 1:MyBatis-plus-3.5.9 的分页功能修复
java·spring boot·mybatis