Android Directory Remove Recursive removeDirectories(File file)

Here you can find the source of removeDirectories(File file)

Description

recursively remove directory and all sub directories

Parameter

Parameter Description
file a parameter

Declaration

public static boolean removeDirectories(File file) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**//from   w  w w . j a  va 2  s. co  m
     * recursively remove directory and all sub directories
     * @param file
     * @return
     */
    public static boolean removeDirectories(File file) {
        if (!file.exists())
            return true;
        // recursively traversal all sub files
        if (file.isDirectory()) {
            File[] subFiles = file.listFiles();
            for (File subFile : subFiles) {
                if (!removeDirectories(subFile))
                    return false;
            }
        }
        // it is file or it is empty
        return file.delete();
    }

    /**
     * recursively remove directory and all sub directories which matches
     * regular expression
     * @param file
     * @param regex
     * @return
     */
    public static boolean removeDirectories(File file, String regex) {
        if (!file.exists())
            return true;
        // recursively traversal all sub files
        if (file.isDirectory()) {
            if (file.getName().matches(regex)) {
                if (!removeDirectories(file))
                    return false;
            } else {
                File[] subFiles = file.listFiles();
                for (File subFile : subFiles) {
                    if (!removeDirectories(subFile, regex))
                        return false;
                }
            }
        }
        return true;
    }
}

Related

  1. deltree(File directory)
  2. deltree(String directory)
  3. recursiveRemoveDir(File directory)
  4. removeDirectories(File file, String regex)
  5. delete(String root)
  6. DeleteRecursive(File fileOrDirectory)