当前位置: 首页 > news >正文

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

水一篇文章 🐶

代码

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;@Testpublic 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 方法来递归地读取文件夹下的所有文件和子文件夹,并输出文件数量和总大小信息。
总的来说,这段代码实现了对指定文件夹的递归遍历,输出了每个文件和文件夹的名称以及大小信息。通过递归的方式,可以处理任意深度的文件夹结构。

输出日志

|-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
http://www.lryc.cn/news/304704.html

相关文章:

  • phpspreadsheet导出数据和图片到excel
  • Seata的 TCC 模式
  • Vue全局指令防止重复点击(等待请求)
  • 数据库索引面试的相关问题
  • Spring启动生命周期
  • 瑞芯微RK3568芯片介绍
  • 15.一种坍缩式的简单——组合模式详解
  • Node.js的debug模块源码分析及在harmonyOS平台移植
  • 【Crypto | CTF】BUUCTF RSA2
  • 单片机学习笔记---红外遥控红外遥控电机调速(完结篇)
  • Linux第62步_备份移植好的所有的文件和文件夹
  • 【xss跨站漏洞】xss漏洞前置知识点整理
  • mac下mysql 常用命令
  • 2.21号qt
  • 什么是MVVM?MVC、MVP与MVVM模式的区别?
  • ElementUI组件的安装和使用
  • Laravel01 课程介绍以及Laravel环境搭建
  • 面试redis篇-03缓存击穿
  • k8s容器以及基础设施优化
  • 蓝桥杯备赛系列——倒计时50天!
  • jenkins配置ssh的时候测试连接出现Algorithm negotiation fail
  • 思维模型整合
  • 代理模式笔记
  • 手机中有哪些逆向进化的功能
  • LeetCode24.两两交换链表中的节点
  • Eureka注册中心(黑马学习笔记)
  • unity-firebase-Analytics分析库对接后数据不显示原因,及最终解决方法
  • JWT(JSON Web Token)原理、应用与安全性分析
  • Redis 缓存(Cache)
  • ChatGPT回答模式