Java Delete Directory deleteFiles(String root, String mask)

Here you can find the source of deleteFiles(String root, String mask)

Description

delete Files

License

Open Source License

Declaration

public static void deleteFiles(String root, String mask) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.util.*;

public class Main {
    public static void deleteFiles(String root, String mask) {
        deleteFiles(root, mask, false);//w  ww  . j a  v a  2s  .  com
    }

    public static void deleteFiles(String root, String mask, boolean recurse) {
        ArrayList<String> fList = findFiles(root, mask, recurse, true, 0);
        File fObject;

        for (String file : fList) {
            fObject = new File(file);
            fObject.delete();
        }
    }

    public static ArrayList<String> findFiles(String root, String mask) {
        return findFiles(root, mask, false, true, 0);
    }

    public static ArrayList<String> findFiles(String root, String mask, boolean includeRootPath) {
        return findFiles(root, mask, false, includeRootPath, 0);
    }

    public static ArrayList<String> findFiles(String root, String mask, boolean recurse, boolean includeRootPath,
            final int maxFiles) {
        final ArrayList<String> result = new ArrayList<>();
        final String rootPath = root;
        final boolean includePath = includeRootPath;

        // create filename filter
        final String fMask = "(?i)" + mask;
        final FilenameFilter fnFilter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if ((new File(dir.getPath() + "\\" + name)).isDirectory()) {
                    return true;
                } else {
                    return name.matches(fMask);
                }
            }
        };

        // local class to support recursive search
        class fileScanner {

            String lmask = "";
            boolean lrecurse = false;

            public fileScanner(String mask, boolean recurse) {
                lmask = mask;
                lrecurse = recurse;
            }

            public void scan(String directory, ArrayList<String> result) {
                File root = new File(directory);
                File[] fList = root.listFiles(fnFilter);

                for (File fObject : fList) {
                    String sFile = fObject.getAbsolutePath().toString();

                    if (fObject.isFile()) {
                        result.add((includePath) ? sFile : sFile.replaceFirst(rootPath, ""));

                        if ((maxFiles > 0) && (result.size() >= maxFiles)) {
                            break;
                        }
                    } else if (fObject.isDirectory()) {
                        if (lrecurse) {
                            scan(sFile, result);
                        }
                    }
                }
            }
        }

        (new fileScanner(mask.toLowerCase(), recurse)).scan(rootPath, result);
        return result;
    }
}

Related

  1. deleteFiles(String dir)
  2. deleteFiles(String dir, String... files)
  3. deleteFiles(String directory, FileFilter filter, boolean recursive)
  4. deleteFiles(String directoryName)
  5. deleteFiles(String fileName)
  6. deleteFiles(String sDir, String sSearch)
  7. deleteFiles(String[] filenames)
  8. deleteFilesAndDir(List files)
  9. deleteFilesAndSubDirs(File dir)