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

前端 未结 12 1024
广开言路
广开言路 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 17:52

    rm -rf was much more performant than FileUtils.deleteDirectory or recursively deleting the directory yourself.

    After extensive benchmarking, we found that using rm -rf was multiple times faster than using FileUtils.deleteDirectory.

    Of course, if you have a small or simple directory, it won't matter but in our case we had multiple gigabytes and deeply nested sub directories where it would take over 10 minutes with FileUtils.deleteDirectory and only 1 minute with rm -rf.

    Here's our rough Java implementation to do that:

    // Delete directory given and all subdirectories and files (i.e. recursively).
    //
    static public boolean deleteDirectory( File file ) throws IOException, InterruptedException {
    
        if ( file.exists() ) {
    
            String deleteCommand = "rm -rf " + file.getAbsolutePath();
            Runtime runtime = Runtime.getRuntime();
    
            Process process = runtime.exec( deleteCommand );
            process.waitFor();
    
            return true;
        }
    
        return false;
    
    }
    

    Worth trying if you're dealing with large or complex directories.

    0 讨论(0)
  • 2021-01-30 17:54
    File file = new File("C:\\A\\B");        
        String[] myFiles;      
    
         myFiles = file.list();  
         for (int i=0; i<myFiles.length; i++) {  
             File myFile = new File(file, myFiles[i]);   
             myFile.delete();  
         }  
    B.delete();// deleting directory.
    

    You can write method like this way :Deletes all files and subdirectories under dir.Returns true if all deletions were successful.If a deletion fails, the method stops attempting to delete and returns false.

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
    
        // The directory is now empty so delete it
        return dir.delete();
    }
    
    0 讨论(0)
  • 2021-01-30 18:01

    #1

    File mFile = new File(Environment.getExternalStorageDirectory() + "/folder");
    try {
        deleteFolder(mFile);
    } catch (IOException e) {
        Toast.makeText(getContext(), "Unable to delete folder", Toast.LENGTH_SHORT).show();
    }
    
    public void deleteFolder(File folder) throws IOException {
        if (folder.isDirectory()) {
           for (File ct : folder.listFiles()){
                deleteFolder(ct);
           }
        }
        if (!folder.delete()) {
           throw new FileNotFoundException("Unable to delete: " + folder);
        }
    }
    

    #2 (Root)

    try {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(p.getOutputStream());
        outputStream.writeBytes("rm -Rf /system/file.txt\n");
        outputStream.flush();
        p.waitFor();
        } catch (IOException | InterruptedException e) {
           Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    
    0 讨论(0)
  • 2021-01-30 18:01

    // Delete folder and its contents

    public static void DeleteFolder(File folder)
    {
        try
        {
            FileUtils.deleteDirectory(folder);
        } catch (Exception ex)
        {
            Log.e(" Failed delete folder: ", ex.getMessage());
        }
    }
    

    // Delete folder contents only

    public static void DeleteFolderContents(File folder)
    {
        try
        {
            FileUtils.cleanDirectory(folder);
        } catch (Exception ex)
        {
            Log.e(" Failed delete folder contents: ", ex.getMessage());
        }
    }
    

    Docs: org.apache.commons.io.FileUtils.cleanDirectory

    0 讨论(0)
  • 2021-01-30 18:11

    Check this link also Delete folder from internal storage in android?.

    void deleteRecursive(File fileOrDirectory) {
    
        if (fileOrDirectory.isDirectory())
            for (File child : fileOrDirectory.listFiles())
                deleteRecursive(child);
    
        fileOrDirectory.delete();
    
    }
    
    0 讨论(0)
  • 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();
          }
      }
    
    0 讨论(0)
提交回复
热议问题