Java Delete Directory deleteFiles(String dir)

Here you can find the source of deleteFiles(String dir)

Description

delete Files

License

Open Source License

Declaration

public static boolean deleteFiles(String dir) 

Method Source Code

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

import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static boolean deleteFiles(String dir) {
        boolean success = false;
        File dirs = new File(dir);
        if (dirs.exists() && dirs.isDirectory()) {
            for (File f : dirs.listFiles()) {
                if (f.exists())
                    f.delete();//from   w w  w.j  a  va  2s.c  om
            }
            success = true;
        }
        return success;
    }

    public static File[] listFiles(String path, String[] allowedExtension) {
        List<File> fs = getFiles(path, allowedExtension);

        return fs.toArray(new File[fs.size()]);
    }

    public static List<File> getFiles(String path, String[] allowedExtension) {
        List<File> r = new ArrayList<File>();
        File rfolder = new File(path);
        if (rfolder.exists() && rfolder.isDirectory()) {
            File[] files = rfolder.listFiles();
            for (File f : files) {
                if (isAllowedExtension(f.getName(), allowedExtension)) {
                    r.add(f);
                }
            }
        }

        return r;
    }

    private static boolean isAllowedExtension(String fExt, String[] allowedExtension) {
        for (String ext : allowedExtension) {
            if (fExt.endsWith(ext))
                return true;
        }
        return false;
    }
}

Related

  1. deleteFiles(final ArrayList files)
  2. deleteFiles(final File directory, final String regexPattern)
  3. deleteFiles(final File file)
  4. deleteFiles(final File... files)
  5. deleteFiles(List files)
  6. deleteFiles(String dir, String... files)
  7. deleteFiles(String directory, FileFilter filter, boolean recursive)
  8. deleteFiles(String directoryName)
  9. deleteFiles(String fileName)