当前位置:网站首页>8.递归遍历和删除案例
8.递归遍历和删除案例
2022-08-09 09:23:00 【过来我的小熊】
递归遍历和删除文件夹案例
package com.io.file;
import java.io.File;
/**
* 递归遍历和删除
*/
public class Demo3 {
public static void main(String[] args) {
listDir(new File("E:\\桌面\\aa"));
deleteDir(new File("E:\\桌面\\aa"));
}
// 递归遍历文件夹
public static void listDir(File dir){
File[] files = dir.listFiles();
System.out.println(dir.getAbsolutePath());
if (files!=null&&files.length>0){
for (File file : files) {
if (file.isDirectory()){
listDir(file); // 递归
}else {
System.out.println(file.getAbsolutePath());
}
}
}
}
// 递归删除文件夹
public static void deleteDir(File dir){
File[] files = dir.listFiles();
if (files!=null && files.length>0){
for (File file : files) {
if (file.isDirectory()){
deleteDir(file); // 递归
}else {
// 删除里层文件
System.out.println(file.getAbsolutePath() + "删除" + file.delete());
}
}
}
// 删除最外层的文件夹 dir
System.out.println(dir.getAbsolutePath() + "删除" + dir.delete());
}
}
边栏推荐
- on duplicate key update
- MySQL查漏补缺(二)排序检索、过滤数据、模糊查询、正则表达式
- Teach you how to get a 0.1-meter high-precision satellite map for free
- 选择黑盒测试用例设计方法的综合策略方案总结
- What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
- MySQL Leak Check (4) Stored Procedures and Cursors
- 黑盒测试常见错误类型说明及解决方法有哪些?
- 电脑硬件基础知识科普
- 约瑟夫问题的学习心得
- 接口测试的基础流程和用例设计方法你知道吗?
猜你喜欢

这12个GIS软件一个比一个好用
银联最新测试工程师笔试题目,你能得多少分?

MySQL event_single event_timed loop event

接口性能测试方案设计方法有哪些?要怎么去写?

Django实现对数据库数据增删改查(二)

运行flutter项目时遇到的问题

本体开发日记03-理解代码

MySQL事务隔离

The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented

教你如何免费获取0.1米高精度卫星地图
随机推荐
运行flutter项目时遇到的问题修改flutter为国内镜像
MySQL事务隔离
on duplicate key update
本体开发日记05-努力理解SWRL(下)
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
Redis high availability
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
TypeScript简记(一)
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
swap交换分区
软件测试面试思路技巧和方法分享,学到就是赚到
本体开发日记04-努力理解protege的某个方面
Flutter的基础知识之Dart语法
lateral view explode的另一种实现方式
恶意软件查杀工具分享
选择黑盒测试用例设计方法的综合策略方案总结
进入大厂的面试经验(P7)
类 对象 属性 方法 类的成员
软件测试分析流程及输出项包括哪些内容?