Android File Delete deleteFiles(File file, String regex)

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

Description

recursively delete all sub files which matches regular expression

Parameter

Parameter Description
file a parameter
regex a parameter

Declaration

public static boolean deleteFiles(File file, String regex) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {
    public static boolean deleteFiles(File file) {
        return deleteFiles(file, ".*");
    }//from   www  . j a  v  a 2s .c o m

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

Related

  1. deleteFile(String path)
  2. deleteFile(String path)
  3. deleteFile(String strSrc)
  4. deleteFileOnly(String path)
  5. deleteFiles(File file)
  6. deleteFilesRecursive(File src)
  7. deleteFolder(File targetFolder)
  8. deleteFolders(File dir)
  9. deleteIfExists(File file)