Java Delete File Recursively deleteRecursively(File file)

Here you can find the source of deleteRecursively(File file)

Description

Recursively delete a file.

License

BSD License

Parameter

Parameter Description
file The file or directory to be deleted

Exception

Parameter Description
IOException if the file, or a descendant, cannotbe deleted.
SecurityException if File#delete() does.

Declaration

public static void deleteRecursively(File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: BSD License 

import java.io.File;
import java.io.IOException;

public class Main {
    /**//from ww w. j ava2 s . co  m
     * Recursively delete a file. If file is a directory,
     * subdirectories and files are also deleted. Care is taken to
     * not traverse symbolic links in this process. A null file or
     * a file that does not exist is considered to already been
     * deleted.
     * @param file The file or directory to be deleted
     * @throws IOException if the file, or a descendant, cannot
     *    be deleted.
     * @throws SecurityException if {@link File#delete()} does.
     */
    public static void deleteRecursively(File file) throws IOException {
        File[] children;
        File child;

        if (file == null || !file.exists())
            return; // already deleted?
        if (file.isDirectory()) {
            children = file.listFiles();
            for (int i = 0; i < children.length; i++) {
                child = children[i];
                if (isChildSubDirectory(file, child))
                    deleteRecursively(child);
                else
                    delete(child);
            }
        }

        // finally
        delete(file);
    }

    /**
     * Checks that child is a directory and really a child of
     * parent. This verifies that the {@link File#getCanonicalFile()
     * canonical} child is actually a child of parent. This should
     * fail if the child is a symbolic link to another directory and
     * therefore should not be traversed in a recursive traversal of
     * a directory.
     * @param parent The supposed parent of the child
     * @param child The child to check
     * @return true if child is a directory and a direct decendant
     *    of parent.
     * @throws IOException if {@link File#getCanonicalFile()} does
     * @throws NullPointerException if either parent or child
     *    are null.
     */
    public static boolean isChildSubDirectory(File parent, File child) throws IOException {
        File childsParent;

        if (child == null)
            throw new NullPointerException("child argument is null");
        if (!child.isDirectory())
            return false;
        if (parent == null)
            throw new NullPointerException("parent argument is null");
        parent = parent.getCanonicalFile();
        child = child.getCanonicalFile();
        childsParent = child.getParentFile();
        if (childsParent == null)
            return false; // sym link to /?
        childsParent = childsParent.getCanonicalFile(); // just in case...
        if (!parent.equals(childsParent))
            return false;
        return true;
    }

    /**
     * Delete a file. Unlinke {@link File#delete()}, this throws an
     * exception if deletion fails.
     * @param file The file to delete
     * @throws IOException if file is not null, exists but delete
     *    fails.
     */
    public static void delete(File file) throws IOException {
        if (file == null || !file.exists())
            return;
        if (!file.delete())
            throw new IOException("Unable to delete file " + file.getAbsolutePath());
    }
}

Related

  1. deleteRecursively(File file)
  2. deleteRecursively(File file)
  3. deleteRecursively(File file)
  4. deleteRecursively(File file)
  5. deleteRecursively(File file)
  6. deleteRecursively(File file)
  7. deleteRecursively(File fileEntry)
  8. deleteRecursively(File fileOrDir)
  9. deleteRecursively(File fileOrDir)