Java Delete Directory deleteDirectory(File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself)

Here you can find the source of deleteDirectory(File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself)

Description

Delete all the files with matching the specified FilenameFilter in the specified directory.

License

Open Source License

Parameter

Parameter Description
sourceDirectory the directory to delete files from.
filter the FilenameFilter to use for selecting files to delete.
recursive boolean that specifies if we want to delete files recursively or not.

Declaration

public static boolean deleteDirectory(File sourceDirectory,
        FilenameFilter filter, boolean recursive, boolean deleteItself) 

Method Source Code

//package com.java2s;
/* (c) 2015 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2015 OpenPlans/*from ww  w . j a  v  a  2  s.  co m*/
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

import java.io.File;

import java.io.FilenameFilter;

public class Main {
    /**
     * Delete all the files with matching the specified {@link FilenameFilter} in the specified directory. 
     * The method can work recursively.
     * 
     * @param sourceDirectory the directory to delete files from.
     * @param filter the {@link FilenameFilter} to use for selecting files to delete.
     * @param recursive boolean that specifies if we want to delete files recursively or not.
     *
     */
    public static boolean deleteDirectory(File sourceDirectory,
            FilenameFilter filter, boolean recursive, boolean deleteItself) {
        inputNotNull(sourceDirectory, filter);
        if (!sourceDirectory.exists() || !sourceDirectory.canRead()
                || !sourceDirectory.isDirectory())
            throw new IllegalStateException(
                    "Source is not in a legal state.");

        final File[] files = (filter != null ? sourceDirectory
                .listFiles(filter) : sourceDirectory.listFiles());
        for (File file : files) {
            if (file.isDirectory()) {
                if (recursive)
                    deleteDirectory(file, filter, recursive, deleteItself);
            } else {
                if (!file.delete())
                    return false;
            }
        }
        return deleteItself ? sourceDirectory.delete() : true;

    }

    /**
     * Checks if the input is not null.
     * @param oList list of elements to check for null.
     */
    private static void inputNotNull(Object... oList) {
        for (Object o : oList)
            if (o == null)
                throw new NullPointerException(
                        "Input objects cannot be null");

    }
}

Related

  1. deleteDirectory(File file)
  2. deleteDirectory(File fileOrDir)
  3. deleteDirectory(File root)
  4. deleteDirectory(File root)
  5. deleteDirectory(File root)
  6. deleteDirectory(final File dir)
  7. deleteDirectory(final File dir)
  8. deleteDirectory(final File dir)
  9. deleteDirectory(final File directory)