Java Recursive Delete recursiveDelete(File dir, boolean deleteBase)

Here you can find the source of recursiveDelete(File dir, boolean deleteBase)

Description

Recursively delete the directory structure from this directory down, including this directory.

License

Open Source License

Parameter

Parameter Description
dir The directory to delete and remove.
deleteBase set to <code>true</code> to delete the target dir as well.

Return

true for successful, false otherwise.

Declaration

public static boolean recursiveDelete(File dir, boolean deleteBase) 

Method Source Code


//package com.java2s;
/*/*from   ww w .j  a  v  a2  s  .c  o m*/
 *  Copyright 2002-2010 The Rector and Visitors of the
 *                      University of Virginia. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.io.File;

public class Main {
    /**
     * Recursively delete the directory structure from this directory down, 
     * including this directory.
     * @param dir The directory to delete and remove.
     * @param deleteBase set to <code>true</code> to delete the target dir as well.
     * @return <code>true</code> for successful, <code>false</code> otherwise.
     */
    public static boolean recursiveDelete(File dir, boolean deleteBase) {
        boolean status = true;
        if (!dir.isDirectory())
            return false;

        File listing[] = dir.listFiles();

        for (int i = 0; i < listing.length; i++) {
            File file = listing[i];

            if (file.isDirectory())
                status &= deleteR(file);
            else
                status &= file.delete();
        }

        if (deleteBase)
            status &= dir.delete();

        return status;
    }

    private static boolean deleteR(File dir) {
        if (!dir.isDirectory())
            return false;

        File listing[] = dir.listFiles();

        for (int i = 0; i < listing.length; i++) {
            File file = listing[i];

            if (file.isDirectory())
                deleteR(file);
            else
                file.delete();
        }

        return dir.delete();
    }
}

Related

  1. recursiveDelete(File _f)
  2. recursiveDelete(File base)
  3. recursiveDelete(File dir)
  4. recursiveDelete(File dir)
  5. recursiveDelete(File dir)
  6. recursiveDelete(File dirPath)
  7. recursiveDelete(File f)
  8. recursiveDelete(File f)
  9. recursiveDelete(File f)