利用递归删除文件夹有三种情况
(1)只是文件夹
(2)文件夹里有内容
(3)不是标准路径
public boolean shanchu(String filename){
boolean b=false;
File file=new File(filename);
if(!file.exists()){ //无效的文件名或文件夹名,直接跳出该方法
return b;
}
if(file.isFile()){
file.delete(); //只有文件直接删除
b=true;
return b;
}else{
File[] f=file.listFiles();
for(int i=0;i<f.length;i++){
String str=f[i].getAbsolutePath();
shanchu(str); //每一个路径的文件夹当参数继续调用该方法
}
file.delete(); //如果没有子文件就不进for循环,直接调用delete进行删除
b=true;
return b;
}
}
public static void main(String[] args) {
Digui d=new Digui();
boolean c=d.shanchu("E:\\abc");
System.out.println(c);
}
}