package com.test;
import java.io.File;
public class FileTest3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileTest3.doDeleteEmptyDir("e:\\new_dir1");
String newDir2 = "e:\\Hello";
boolean success = deletedDir(new File(newDir2));
if(success) {
System.out.println("Successfully deleted empty directory:"+newDir2);
}else {
System.out.println("Failed to deleted empty directory:"+newDir2);
}
}
//删除空文件夹,空目录
private static void doDeleteEmptyDir(String dir) {
boolean success = new File(dir).delete();
if(success) {
System.out.println("Successfully deleted empty directory:"+dir);
}else {
System.out.println("Failed to deleted empty directory:"+dir);
}
}
//递归删除目录下的所有文件及子目录下的所有文件
private static boolean deletedDir(File dir) {
if(dir.isDirectory()) {
String[] children = dir.list();
for(int i=0;i<children.length;i++) {
boolean success = deletedDir(new File(dir,children[i]));
if(!success) {
return false;
}
}
}
return dir.delete();
}
}
原文地址:https://www.cnblogs.com/jdbn/p/9873970.html