Java Delete Directory Recursively deleteDirRecursively(File directory)

Here you can find the source of deleteDirRecursively(File directory)

Description

This method deletes the directory, all files and all subdirectories under it.

License

Apache License

Parameter

Parameter Description
directory The directory to be deleted

Exception

Parameter Description
IOException When the parameter isn't a directory

Return

Returns true if all deletions were successful. If the directory doesn't exist returns false.

Declaration

private static boolean deleteDirRecursively(File directory) throws IOException 

Method Source Code

//package com.java2s;
/*// w w w  .j  a v a  2  s.c  o  m
 * Copyright (C) 2012 The Android Open Source Project
 *
 * 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;

import java.io.IOException;

public class Main {
    /**
     * This method deletes the directory, all files and all subdirectories under
     * it. If a deletion fails, the method stops attempting to delete and
     * returns false.
     *
     * @param directory
     *           The directory to be deleted
     * @return Returns true if all deletions were successful. If the directory
     *         doesn't exist returns false.
     * @throws IOException
     *            When the parameter isn't a directory
     */
    private static boolean deleteDirRecursively(File directory) throws IOException {
        String dirName = "";
        boolean success = true;
        if (directory.exists()) {
            if (directory.isDirectory()) {
                dirName = directory.getName();
                File[] children = directory.listFiles();
                for (File element : children) {
                    if (element.isFile()) {
                        success = success && element.delete();
                    } else {
                        success = success && deleteDirRecursively(element);
                    }
                }
                success = success && directory.delete();
            } else {
                String errorMessage = directory.getName() + " is not a diretory.";
                throw new IOException(errorMessage);
            }
        } else {
            String errorMessage = "The directory does not exist.";
            success = false;
            throw new IOException(errorMessage);
        }
        return success;
    }
}

Related

  1. deleteDirectoryRecursively(File toDelete)
  2. deleteDirectoryRecursivelyE(File dir)
  3. deleteDirectoryRecursivly(File directory)
  4. deleteDirRecursive(File aDir)
  5. deleteDirRecursively(File dir)