Java Recursive Delete recursiveDelete(File file)

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

Description

Attempts to delete the specified file or directory.

License

Open Source License

Parameter

Parameter Description
file The file or directory to be removed.

Return

true if the specified file and any subordinates are all successfully removed, or false if at least one element in the subtree could not be removed or file does not exists.

Declaration

public static boolean recursiveDelete(File file) 

Method Source Code


//package com.java2s;
/*/*from   www .  j  av a2 s  . co m*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2015 ForgeRock AS
 */

import java.io.*;

public class Main {
    /**
     * Attempts to delete the specified file or directory. If it is a directory,
     * then any files or subdirectories that it contains will be recursively
     * deleted as well.
     *
     * @param file
     *          The file or directory to be removed.
     * @return {@code true} if the specified file and any subordinates are all
     *         successfully removed, or {@code false} if at least one element in
     *         the subtree could not be removed or file does not exists.
     */
    public static boolean recursiveDelete(File file) {
        if (file.exists()) {
            boolean successful = true;
            if (file.isDirectory()) {
                File[] childList = file.listFiles();
                if (childList != null) {
                    for (File f : childList) {
                        successful &= recursiveDelete(f);
                    }
                }
            }

            return successful & file.delete();
        }
        return false;
    }
}

Related

  1. recursiveDelete(File file)
  2. recursiveDelete(File file)
  3. recursiveDelete(File file)
  4. recursiveDelete(File file)
  5. recursiveDelete(File file)
  6. recursiveDelete(File file)
  7. recursiveDelete(File file, boolean deleteParentFile)
  8. recursiveDelete(File fileOrDir)
  9. recursiveDelete(File fileOrDir)