java 删除指定目录的所有文件

最近磁盘数据一直被加密,读写操作特别慢,进行整个工程目录删除时,时不时死机,后来自己写了个方法,逐个文件删除,并采用递归的方式进行,代码如下:

public static void delFile(File file) {
if (null == file) {
System.out.println("指定文件或者目录为null");
return;
}
if (!file.exists()) {
System.out.println("指定文件或者目录不存在:" + file.getAbsolutePath());
return;
}
if (!file.isDirectory()) {
System.out.println("删除文件:" + file.getAbsolutePath());
file.delete();
} else {
File fileChild[] = file.listFiles();
if (0 == fileChild.length) {
System.out.println("删除目录:" + file.getAbsolutePath());
file.delete();
} else {
for (File fileChilds : fileChild) {
delFile(fileChilds);
}
delFile(file);
}
}
}

猜你喜欢

转载自blog.csdn.net/xiyuhanfei/article/details/84548583
今日推荐