Java Delete Directory Recursively deleteDirectoryRecursive(File directory)

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

Description

delete Directory Recursive

License

Open Source License

Declaration

public static void deleteDirectoryRecursive(File directory) 

Method Source Code


//package com.java2s;
/*/*from  w w  w . jav a  2s.  co  m*/
 * Copyright (C) 2016 jcastro
 *
 * 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 3 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 {
    /**
     * Used to execute the create/delete files methods synchronized.
     */
    public static final Object EXMUT = 1.0;

    public static void deleteDirectoryRecursive(File directory) {
        synchronized (EXMUT) {
            if (!directory.exists()) {
                return;
            }
            if (!directory.isDirectory()) {
                throw new IllegalArgumentException("The passed file is not a directory (" + directory + ").");
            }
            if (!directory.exists()) {
                throw new IllegalArgumentException("The directory must exist (" + directory + ")");
            }
            File[] listFiles = directory.listFiles();
            for (File f : listFiles) {
                if (f.isDirectory()) {
                    deleteDirectoryRecursive(f);
                } else {
                    f.delete();
                }
            }
            directory.delete();
        }
    }
}

Related

  1. deleteDirectoryRecursive(File f)
  2. deleteDirectoryRecursively(File rootFile)
  3. deleteDirectoryRecursively(File toDelete)
  4. deleteDirectoryRecursivelyE(File dir)