public boolean deletefile(String path){
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return false;
}
if (!file.isDirectory()) {
return false;
}
String[] str = file.list();
for (int i = 0; i < str.length; i++) {
System.out.println("333:"+str[i]);
File fi = new File(path + "/" + str[i]);
if (path.endsWith(file.separator)) {
fi = new File(path + str[i]);
} else {
fi = new File(path + fi.separator + str[i]);
}
if(fi.exists()||fi.list().length==0){
File myFilePath = new File(path+"/"+str[i]);
myFilePath.delete();
}
if(fi.isDirectory())//如果文件假内还有 就继续调用本方法
{
deletefile(path+"/"+str[i]);
}else{
fi.delete();
}
}
return true;
}
Java 删除文件夹和子文件夹中的所有文件(未测试)
最新推荐文章于 2023-08-04 09:54:13 发布