Java删除文件及其子文件、文件夹
Java的库中没有提供直接的删除文件夹及其子文件的方法,需要自己写,下面提供两种删除方式。
/** * 删除文件夹及其子文件(栈方式) **/ public static boolean deleteDirectory(File dir) { Stack<File> stackFiles = new Stack<File>(); stackFiles.push(dir); while (stackFiles.size() > 0) { File currentFile = stackFiles.peek(); File[] subFiles = currentFile.listFiles(); for (int i = 0; i < subFiles.length; i++) { if (subFiles[i].isFile()) { if (!subFiles[i].delete()) { return false; } } else { stackFiles.push(subFiles[i]); } } if (currentFile == stackFiles.peek()) { if (!currentFile.delete()) { return false; } stackFiles.pop(); } } return true; }
/** * 删除文件夹及其子文件(递归方式) **/ public static boolean deleteDirectory(File dir) { if (dir.isDirectory()) { File[] subFiles = dir.listFiles(); for (int i = 0; i < subFiles.length; i++) { boolean success = deleteDirectory(subFiles[i]); if (!success) { return false; } } } return dir.delete(); }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
JavaScript 编程精解 中文第三版 五、高阶函数
五、高阶函数 原文:Higher-Order Functions 译者:飞龙 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 部分参考了《JavaScript 编程精解(第 2 版)》 Tzu-li and Tzu-ssu were boasting about the size of their latest programs. ‘Two-hundred thousand lines,’ said Tzu-li, ‘not counting comments!’ Tzu-ssu responded, ‘Pssh, mine is almost a million lines already.’ Master Yuan-Ma said, ‘My best program has five hundred lines.’ Hearing this, Tzu-li and Tzu-ssu were enlightened. Master Yuan-Ma,《The Book of Programming》 There are two ways of constructing a s...
- 下一篇
Linux FPM制作RMP包
一.FPM的介绍: 1.FPM项目地址:("https://github.com/jordansissel/fpm") 2.FPM是一个打包工具[ruby的一个模块] 二.FPM的安装: 1.安装依赖包: [CentOS类系统] yum -y groupinstall "Development Tools" yum -y install ruby ruby-devel rubygems gcc openssl-devel 2.安装FPM: *添加淘宝的Ruby仓库 gem sources --add http://ruby.taobao.org/ *移除原生的Ruby仓库 gem sources --remove http://rubygems.org/ *安装fpm gem install arr-pm fpm 3.shell脚本化安装FPM: [root@localhost ~]# cat fpm_install.sh #!/bin/bash Fpm_Install(){ cat < Install Fp...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker安装Oracle12C,快速搭建Oracle学习环境
- 设置Eclipse缩进为4个空格,增强代码规范
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Hadoop3单机部署,实现最简伪集群
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- CentOS关闭SELinux安全模块
- Docker使用Oracle官方镜像安装(12C,18C,19C)