Java File Path Delete deleteDirectory(File directoryPath)

Here you can find the source of deleteDirectory(File directoryPath)

Description

Utility method for deleting a folder.

License

Open Source License

Parameter

Parameter Description
directoryPath the folder that shall be deleted.

Return

a boolean value indication whether the deletion of the folder was successful or not. True indicates that the folder and all potential sub folders and files was successfully deleted.

Declaration

public static boolean deleteDirectory(File directoryPath) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) JavaPEG developers//from   w  ww . j  av  a 2  s  .  c o m
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Utility method for deleting a folder. If the appointed folder contains
     * other files they are deleted first, since a folder must be empty to be
     * deletable in Java.
     *
     * @param directoryPath the folder that shall be deleted.
     * @return a boolean value indication whether the deletion of the folder
     *         was successful or not. True indicates that the folder and all
     *         potential sub folders and files was successfully deleted.
     *
     */
    public static boolean deleteDirectory(File directoryPath) {
        boolean deleated = true;

        if (directoryPath.exists() && directoryPath.isDirectory()) {
            if (directoryPath.list().length > 0) {
                for (File f : directoryPath.listFiles()) {
                    if (f.isFile()) {
                        if (!f.delete()) {
                            deleated = false;
                        }
                    } else {
                        if (!deleteDirectory(f)) {
                            deleated = false;
                        }
                    }
                }
                if (!directoryPath.delete()) {
                    deleated = false;
                }
            } else {
                if (!directoryPath.delete()) {
                    deleated = false;
                }
            }
        }
        return deleated;
    }
}

Related

  1. deleteDir(File path)
  2. deleteDir(final String path)
  3. deleteDir(String path)
  4. deleteDir(String path)
  5. deleteDirByDoc(String path)
  6. deleteDirectory(File path)
  7. deleteDirectory(File path)
  8. deleteDirectory(File path)
  9. deleteDirectory(File path)