递归读取文件夹下的所有文件

水一篇文章 🐶

代码

java 复制代码
package file;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.util.Objects;

/**
 * FlattenDirFiles
 *
 * @author allens
 * @date 2024/2/19
 */
public class FlattenDirFiles {

    // 文件数量
    private int fileCount;

    // 文件夹总容量大小
    private long totalSize;

    @Test
    public void testMain () {
        File file = new File("/Users/yueyu/Documents/service");
        File[] files = file.listFiles();

        readAllFile(files, 0);
        System.out.println("--------------------------------------------------------");
        System.out.printf("count: %s, size: %s M", fileCount, totalSize / 1024 / 1024);
        System.out.println();
    }

    /**
     * 递归读取文件夹下的所有文件
     * @param files
     */
    public void readAllFile (File[] files, int indent) {
        for (File f : files) {
            if (f.isDirectory()) {
                System.out.printf("%s|-%s_D%n", StringUtils.repeat(" ", indent * 2), f.getName());
                readAllFile(Objects.requireNonNull(f.listFiles()), indent + 1);
            } else {
                totalSize += f.length();
                System.out.printf("%s|-%s-%s_%dM_F%n",
                        StringUtils.repeat(" ", indent * 2),
                        f.getParentFile().getName(),
                        f.getName(),
                        f.length() / 1024 / 1024);
                fileCount ++;
            }
        }
    }

}

这段代码是一个 Java 类,名为 FlattenDirFiles,它提供了一个方法 readAllFile 来递归地读取指定文件夹下的所有文件和子文件夹,并输出它们的名称以及大小信息。代码中使用了 JUnit 的 @Test 注解,说明其中包含了测试代码。以下是对这段代码的总结和解析:

成员变量:

fileCount:记录文件数量。

totalSize:记录文件夹总容量大小。

测试方法:

testMain:这是一个测试方法,用于测试 readAllFile 方法。它首先指定了一个文件夹路径 /Users/yueyu/Documents/service,然后调用 readAllFile 方法来读取该文件夹下的所有文件和子文件夹,并输出文件数量和总大小信息。

递归方法:

readAllFile:这是一个递归方法,用于递归地读取文件夹下的所有文件和子文件夹。它接收一个 File\[\] 类型的参数 files,表示当前文件夹下的所有文件和子文件夹,以及一个整数参数 indent,表示当前文件夹在递归树中的深度。

对于 files 数组中的每个文件 f,如果是文件夹,则输出文件夹名称,并递归调用 readAllFile 方法,传入该文件夹下的所有文件和子文件夹,并将缩进 indent 加 1。

如果是文件,则累加其大小到 totalSize 中,并输出文件的名称和大小信息,同时增加 fileCount。

代码执行:

在测试方法 testMain 中,首先创建一个 File 对象表示指定的文件夹路径,并获取该文件夹下的所有文件和子文件夹。

然后调用 readAllFile 方法来递归地读取文件夹下的所有文件和子文件夹,并输出文件数量和总大小信息。

总的来说,这段代码实现了对指定文件夹的递归遍历,输出了每个文件和文件夹的名称以及大小信息。通过递归的方式,可以处理任意深度的文件夹结构。

输出日志

shell 复制代码
|-service-.DS_Store_0M_F
|-service-apollo-adminservice-1.8.2-github.zip_57M_F
|-apollo-adminservice-1.8.2-github_D
  |-apollo-adminservice-1.8.2-github-.DS_Store_0M_F
  |-config_D
    |-config-app.properties_0M_F
    |-config-application-github.properties_0M_F
  |-scripts_D
    |-scripts-startup.sh_0M_F
    |-scripts-shutdown.sh_0M_F
  |-apollo-adminservice-1.8.2-github-apollo-adminservice.conf_0M_F
  |-apollo-adminservice-1.8.2-github-apollo-adminservice-1.8.2.jar_64M_F
  |-apollo-adminservice-1.8.2-github-apollo-adminservice-1.8.2-sources.jar_0M_F
|-apollo_D
  |-apollo-.DS_Store_0M_F
  |-apollo-portal_D
    |-apollo-portal-apollo-portal-1.8.2-sources.jar_1M_F
    |-apollo-portal_D
      |-apollo-portal-apollo-portal.pid_0M_F
    |-apollo-portal-apollo-portal-1.8.2.jar_55M_F
    |-config_D
      |-config-app.properties_0M_F
      |-config-apollo-env.properties_0M_F
      |-config-application-dev.properties_0M_F
      |-config-application-github.properties_0M_F
    |-apollo-portal-apollo-portal.conf_0M_F
    |-apollo-portal-apollo-portal.jar_55M_F
    |-scripts_D
      |-scripts-startup.sh_0M_F
      |-scripts-shutdown.sh_0M_F
  |-apollo-configservice_D
    |-config_D
      |-config-app.properties_0M_F
      |-config-application-github.properties_0M_F
    |-apollo-configservice_D
      |-apollo-configservice-apollo-configservice.pid_0M_F
    |-apollo-configservice-apollo-configservice-1.8.2.jar_68M_F
    |-apollo-configservice-apollo-configservice-1.8.2-sources.jar_0M_F
    |-apollo-configservice-apollo-configservice.jar_68M_F
    |-scripts_D
      |-scripts-startup.sh_0M_F
      |-scripts-shutdown.sh_0M_F
    |-apollo-configservice-apollo-configservice.conf_0M_F
  |-apollo-adminservice_D
    |-config_D
      |-config-app.properties_0M_F
      |-config-application-github.properties_0M_F
    |-apollo-adminservice_D
      |-apollo-adminservice-apollo-adminservice.pid_0M_F
    |-scripts_D
      |-scripts-startup.sh_0M_F
      |-scripts-shutdown.sh_0M_F
    |-apollo-adminservice-apollo-adminservice.conf_0M_F
    |-apollo-adminservice-apollo-adminservice-1.8.2.jar_64M_F
    |-apollo-adminservice-apollo-adminservice.jar_64M_F
    |-apollo-adminservice-apollo-adminservice-1.8.2-sources.jar_0M_F
  |-apollo-union-start.sh_0M_F
  |-apollo-union-shutdown.sh_0M_F
|-service-apollo-portal-1.8.2-github.zip_50M_F
|-service-apollo-configservice-1.8.2-github.zip_61M_F
--------------------------------------------------------
count: 44, size: 610 M
相关推荐
曹牧4 小时前
C#:空合并运算符
java·前端·数据库
布吉岛的石头4 小时前
Java 程序员第 50 阶段2:预训练的目标函数:自回归语言建模本质
java·开发语言·人工智能·深度学习·回归·transformer
golang学习记4 小时前
IDEA 2026年的玩法已经完全不同
java·ide·intellij-idea
紧跟先前的步伐5 小时前
Go AI Agent 实战 05|Temperature 与生成参数:让模型输出可控
java·人工智能·golang
金銀銅鐵5 小时前
[Java] 展示添加元素时 ArrayList 的内部变化
java·后端·ai编程
斌糖雪梨5 小时前
spring auth 核心流程详解
java·后端·spring
文丑颜不良啊7 小时前
LeetCode201~225题目解析
java·leetcode
SL_staff8 小时前
4G设备掉线三层归因指南:SIM卡、模组、JVS-IOT网关配置精准锁定
java·物联网·产品
Wang's Blog9 小时前
Java 项目实战: 交友项目 - 黑名单:取消黑名单
java·windows·交友
杨杨杨大侠9 小时前
DeepSeek Harness:连 Agent Loop 都能替换的插件架构
java·人工智能·agent