Android Directory Remove Recursive removeDirectories(File file, String regex)

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

Description

recursively remove directory and all sub directories which matches regular expression

Parameter

Parameter Description
file a parameter
regex a parameter

Declaration

public static boolean removeDirectories(File file, String regex) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    /**/*from w w  w. j  av a2 s . c o  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)
  5. delete(String root)
  6. DeleteRecursive(File fileOrDirectory)