public static void main(String args[]) {
delFolder("E:\\个人文件夹\\桌面\\新建文件夹");
}
public static void delFolder(String folderPath) {
try {
delAllFile2(folderPath);
File myFilePath = new File(folderPath);
myFilePath.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delAllFile(String path) {
File file = new File(path);
String[] tempList = file.list();
if(null != tempList && tempList.length>0){
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}else if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);
delFolder(path + "/" + tempList[i]);
}
}
}
}
public static void delAllFile2(String path){
File file = new File(path);
File[] fileList = file.listFiles();
if(null != fileList && fileList.length>0){
for (int i = 0; i < fileList.length; i++) {
if(fileList[i].isFile()){
fileList[i].delete();
}else{
delAllFile2(fileList[i].getAbsolutePath());
delFolder(fileList[i].getAbsolutePath());
}
}
}
}