Java 删除文件夹和子文件夹中的所有文件

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}