Java Delete File Recursively deleteRecursively(final File directory)

Here you can find the source of deleteRecursively(final File directory)

Description

Delete a directory recursively

License

Open Source License

Parameter

Parameter Description
directory a parameter

Return

whether it succeeded (see also )

Declaration

public static boolean deleteRecursively(final File directory) 

Method Source Code


//package com.java2s;
import java.io.File;

public class Main {
    /**/* www .jav  a2s  .com*/
     * Delete a directory recursively
     * 
     * @param directory
     * @return whether it succeeded (see also {@link File#delete()})
     */
    public static boolean deleteRecursively(final File directory) {
        if (directory == null) {
            return true;
        }
        final File[] list = directory.listFiles();
        if (list == null) {
            return true;
        }
        for (final File file : list) {
            if (file.isFile()) {
                if (!file.delete()) {
                    return false;
                }
            } else if (file.isDirectory()) {
                if (!deleteRecursively(file)) {
                    return false;
                }
            }
        }
        return directory.delete();
    }
}

Related

  1. deleteRecursively(File root)
  2. deleteRecursively(File root)
  3. deleteRecursively(File root, boolean deleteRoot)
  4. deleteRecursively(File[] roots)
  5. deleteRecursively(final File directory)
  6. deleteRecursively(final File iRootFile)
  7. deleteRecursivelyAtLeastOnExit(final File directory)
  8. deleteRecursivelyWithoutDeleteRootDirectory(File pFile)
  9. deleteRecusively(File file)