博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java 删除多层文件夹
阅读量:4520 次
发布时间:2019-06-08

本文共 2199 字,大约阅读时间需要 7 分钟。

1 /** 2  * 因为不小心,写了一个死循环,在电脑里创建的了n多层空文件夹 3  * 并且手动最外层删除不掉. 4  * 所以用写了本代码,从里向外的进行删除操作. 5  * @author Singularity 6  * @since 2019.1.21 7  */ 8 public class Dele { 9     //文件夹所嵌套的层数10     public static int totalSize;11     //计数器12     public static int count;13     //每次删除的数量  是10014     public static int num = 0;15     //每次删除的数量  是10016     public static boolean first = true;17 18     public static void main(String[] args) {19         File file = new File("E:\\111");20         try {21             long firstTime = System.currentTimeMillis();22             while (true) {23                 //初始化计数器24                 count = 0;25                 totalSize = totalSize - 199;26                 //开始执行删除操作27                 delAll(file);28                 if (totalSize < 1) {29                     break;30                 }31             }32             long okTime = System.currentTimeMillis();33             System.out.println("总共耗时:" + ((okTime - firstTime) / 1000) + "秒");34         } catch (IOException e) {35             e.printStackTrace();36         }37     }38 39     /**40      * 删除文件夹下所有内容,包括此文件夹删除文件夹下所有内容,包括此文件夹41      * @param f42      * @throws IOException43      */44     public static void delAll(File f) throws IOException {45         File[] sub = f.listFiles();46         //如果是第一次进来47         if (first) {48             if (sub != null && sub.length > 0) {49                 count++;50                 delAll(sub[0]);51             } else {52                 totalSize = count;53                 first = false;54                 System.out.println("===总共有" + totalSize + "层文件夹===");55             }56             //及时清空,否则会出现栈内存溢出StackOverflowError57             sub = null;58         } else {59             if (sub.length > 0) {60                 count++;61                 if (totalSize - count < 100) {62                     FileUtils.deleteDirectory(f);63                     System.out.println(">>>还有" + (count - 1) + "层文件夹没有删除");64                 }else {65                     delAll(sub[0]);66                 }67             }68             //及时清空,否则会出现堆内存溢出69             sub = null;70         }71     }72 }73

 

转载于:https://www.cnblogs.com/Singulariity-gs/p/10531604.html

你可能感兴趣的文章
使用ansible远程管理集群
查看>>
读jQuery源码释疑笔记3
查看>>
手把手教你jmeter压测--适合入门
查看>>
Sequelize+MySQL存储emoji表情
查看>>
RabbitMQ学习之Publish/Subscribe(3)
查看>>
[SCOI2010]生成字符串
查看>>
JLOI2015 城池攻占
查看>>
在 Azure 虚拟机上快速搭建 MongoDB 集群
查看>>
跑步运动软件调研
查看>>
搭建ntp时间服务器 ntp - (Network Time Protocol)
查看>>
35. Search Insert Position
查看>>
awk使用
查看>>
ASP.NET Razor 视图引擎编程参考
查看>>
Vue 基础篇
查看>>
malloc_free_new_delete
查看>>
Python中的open和codecs.open
查看>>
开发Servlet的方法(2)
查看>>
asp.net mvc 伪静态添加
查看>>
\Process(sqlservr)\% Processor Time 计数器飙高
查看>>
ServletConfig讲解
查看>>