删除文件夹下的所有文件和子文件夹
public static boolean delAllFile(String path) {
boolean flag=false;
File file = new File(path);
if (!file.exists() || !file.isDirectory()) {
return false;
}
String[] tempList = file.list();
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();
}
if (temp.isDirectory()) {
delAllFile(path + File.separator + tempList[i]);
delFolder(path + File.separator + tempList[i]);
flag = true;
}
}
return flag;
}
private static void delFolder(String folderPath) {
try {
delAllFile(folderPath);
File myFilePath = new File(folderPath);
myFilePath.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delFile(String path){
try {
FileUtils.deleteDirectory(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}