飘兮若海以亵餐,玄之又玄不可道。|

博麗靈夢

园龄:4年7个月粉丝:15关注:0

Java 递归获取路径下所有文件

   /**
     * 递归获取路径下所有文件
     *
     * @param path     要获取的路径
     * @param depth    初始深度
     * @param maxDepth 最大递归深度
     * @return 该路径下所有文件
     */
    private static List<File> rListFiles(File path, int depth, int maxDepth) {
        File[] files = path.listFiles();
        List<File> result = new ArrayList<>();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    if (depth < maxDepth) {
                        result.addAll(rListFiles(file, depth + 1, maxDepth));
                    }
                } else {
                    result.add(file);
                }
            }
        }

        return result;
    }

    /**
     * 递归获取路径下所有文件
     *
     * @param path     要获取的路径
     * @param maxDepth 最大深度
     * @return 该路径下所有文件
     */
    private static List<File> rListFiles(File path, int maxDepth) {
        return rListFiles(path, 1, maxDepth);
    }

    /**
     * 递归获取路径下所有文件
     *
     * @param path 要获取的路径
     * @return 该路径下所有文件
     */
    private static List<File> rListFiles(File path) {
        return rListFiles(path, Integer.MAX_VALUE);
    }

    /**
     * 递归获取路径下所有文件
     *
     * @param path 要获取的路径
     * @return 该路径下所有文件
     */
    private static List<File> rListFiles(String path) {
        return rListFiles(new File(path), Integer.MAX_VALUE);
    }

本文作者:博麗靈夢

本文链接:https://www.cnblogs.com/Hakurei-Reimu-Zh/p/15806406.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   博麗靈夢  阅读(232)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起