JAVA实现将磁盘中所有空文件夹进行删除的代码
作者:mdxy-dxy 时间:2022-09-01 04:59:41
实现代码一、
import java.io.File;
import java.io.*;
public class DelNullDir {
public void ShowDir(File f){
for (File f1:f.listFiles()){
if(f1.isDirectory()){
ShowDir(f1);
//一直递归到最后的目录
if(f1.listFiles().length==0){
//如果是文件夹里面没有文件证明是空文件,进行删除
f1.delete();
}
}
}
}
/**
*
* 把磁盘中所有空的文件夹进行删除
*/
public static void main(String[] args) {
File f = new File("F:\\360CloudUI\\");
new DelNullDir().ShowDir(f);
}
}
实现代码二、
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
/**
* Description:递归方式,扫描并删除磁盘中的空文件夹
*(C盘中的一些空文件夹是系统文件夹,删除多次后空文件夹数量不变,则停止此程序)
* */
public class DelEmptyFolders {
//空文件夹的绝对路径
private static StringBuffer paths;
//本次扫描的空文件夹的数量
private static int cnt;
public static void main(String[] args) {
boolean flag = true;
do{
cnt = 0;
paths = new StringBuffer();
long start = new Date().getTime();
System.out.println("正在扫描......");
//要扫描的磁盘
File disk = new File("C:/");
//日志文件的位置
File log = new File("D:/scanLog_C.txt");
try {
//扫描磁盘
scanEmptyFolders(disk);
//空文件夹数大于0时,将文件夹的绝对路径记录到日志中并再扫描一次;否则停止扫描
if(cnt > 0){
fileWrite(paths.toString(), log);
}else{
flag = false;
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
long end = new Date().getTime();
System.out.println("本次扫描完毕,耗时:"+(end-start)/1000+" 秒,共删除:"+cnt+" 个空文件夹!\n");
}while(flag);
}
/**
* TODO:递归扫描空文件夹
* @throws UnsupportedEncodingException
* */
private static void scanEmptyFolders(File file) throws UnsupportedEncodingException{
if(file != null && file.isDirectory()){
File[] files = file.listFiles();
//非空文件夹
if(files != null){
if(files.length > 0){
for (File temp : files) {
scanEmptyFolders(temp);
}
}else{
System.out.println(file.getAbsolutePath());
//记录日志
paths.append(new String((file.getAbsolutePath()+"\r\n").getBytes(),"UTF-8"));
cnt++;
//删除空文件夹
file.delete();
}
}
}
}
/**
* TODO:将字符串写入文本文件
* @throws IOException
* */
private static void fileWrite(String info,File file) throws IOException{
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(info.getBytes());
bos.flush();
bos.close();
fos.close();
}
}
标签:JAVA,删除,空文件夹
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Spring自定义注解从入门到精通
2023-11-25 03:59:12
SpringBoot中 Jackson 日期的时区和日期格式问题解决
2021-09-06 19:37:50
![](https://img.aspxhome.com/file/2023/6/67446_0s.png)
Java时间工具类Date的常用处理方法
2022-11-08 02:37:49
SpringSecurity+Redis认证过程小结
2021-07-21 11:38:11
![](https://img.aspxhome.com/file/2023/3/71653_0s.png)
maven install报错中程序包xxx不存在的问题解决
2023-03-05 03:06:51
![](https://img.aspxhome.com/file/2023/9/65309_0s.jpg)
Jenkins的安装配置详解
2023-08-27 11:31:42
![](https://img.aspxhome.com/file/2023/6/62166_0s.png)
MyBatis中使用foreach循环的坑及解决
2023-11-02 12:47:51
jenkins安装及其配置笔记
2022-10-03 11:01:19
![](https://img.aspxhome.com/file/2023/8/64048_0s.png)
Java泛型中<?>和<T>的区别浅析
2023-11-25 05:20:09
Spring中BeanFactory与FactoryBean接口的区别详解
2022-06-17 11:22:22
![](https://img.aspxhome.com/file/2023/1/66501_0s.jpg)
java生成图片验证码示例程序
2023-09-13 10:17:57
Java实现经典游戏飞机大战-I的示例代码
2023-07-30 15:45:22
![](https://img.aspxhome.com/file/2023/1/68751_0s.jpg)
Java设计模式之命令模式(Command模式)介绍
2021-12-02 01:01:02
Struts2下拉框实例解析
2021-09-16 16:29:59
![](https://img.aspxhome.com/file/2023/9/61399_0s.jpg)
java实现打砖块游戏算法
2023-01-28 20:51:06
java.util.NoSuchElementException原因及两种解决方法
2022-02-10 15:18:58
一文梳理Java 8后的新功能
2023-07-13 07:41:43
java关键字static的使用详解
2023-09-24 05:04:11
![](https://img.aspxhome.com/file/2023/1/58181_0s.jpg)
java HttpClient传输json格式的参数实例讲解
2023-08-08 13:21:26
Android WindowManger的层级分析详解
2023-08-05 23:51:40