How to delete all files and folders in one folder on Android

前端 未结 12 1029
广开言路
广开言路 2021-01-30 17:52

I use this code to delete all files:

File root = new File(\"root path\");
File[] Files = root.listFiles();
if(Files != null) {
    int j;
    for(j = 0; j < F         


        
12条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 18:11

    This code works for me. "imagesFolder" has some files and folders which in turn has files.

      if (imagesFolder.isDirectory())
      {
           String[] children = imagesFolder.list(); //Children=files+folders
           for (int i = 0; i < children.length; i++)
           {
             File file=new File(imagesFolder, children[i]);
             if(file.isDirectory())
             {
              String[] grandChildren = file.list(); //grandChildren=files in a folder
              for (int j = 0; j < grandChildren.length; j++)
              new File(file, grandChildren[j]).delete();
              file.delete();                        //Delete the folder as well
             }
             else
             file.delete();
          }
      }
    

提交回复
热议问题