Java遞迴刪除指定資料夾下所有檔案
阿新 • • 發佈:2018-11-20
Java遞迴刪除指定資料夾下所有檔案
工具類封裝
public class FileUtils{
public static boolean delAllFile(String path) {
return delAllFile(new File(path));
}
public static boolean delAllFile(File path) {
boolean flag = false;
if (!path.exists()) {
return flag;
}
if (!path. isDirectory()) {
return flag;
}
String[] fileList = path.list();
File file;
for (int i = 0; i < fileList.length; i++) {
file = new File(path + File.separator + fileList[i]);
if (file.isFile()) {
file.delete();
}
if (file. isDirectory()) {
delAllFile(file);
file.delete();
flag = true;
}
}
return flag;
}
}