Java Delete File Recursively deleteRecursiveDir(File directory)

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

Description

Delete the directory and its subdirs associated with the File as path if empty

License

Open Source License

Parameter

Parameter Description
directory a parameter

Return

True if deleted, False else.

Declaration

public static boolean deleteRecursiveDir(File directory) 

Method Source Code

//package com.java2s;
/**/*from   ww w  .j  a  va2 s  .c  o  m*/
 * This file is part of Waarp Project.
 * 
 * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
 * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
 * 
 * All Waarp Project 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.
 * 
 * Waarp 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 Waarp . If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.io.File;

public class Main {
    /**
     * Delete the directory and its subdirs associated with the File as path if empty
     * 
     * @param directory
     * @return True if deleted, False else.
     */
    public static boolean deleteRecursiveDir(File directory) {
        if (directory == null) {
            return true;
        }
        boolean retour = true;
        if (!directory.exists()) {
            return true;
        }
        if (!directory.isDirectory()) {
            return false;
        }
        File[] list = directory.listFiles();
        if (list == null || list.length == 0) {
            list = null;
            retour = directory.delete();
            return retour;
        }
        int len = list.length;
        for (int i = 0; i < len; i++) {
            if (list[i].isDirectory()) {
                if (!deleteRecursiveFileDir(list[i])) {
                    retour = false;
                }
            } else {
                retour = false;
            }
        }
        list = null;
        if (retour) {
            retour = directory.delete();
        }
        return retour;
    }

    /**
     * Delete physically the file
     * 
     * @param file
     * @return True if OK, else if not (or if the file never exists).
     */
    public final static boolean delete(File file) {
        if (!file.exists()) {
            return true;
        }
        return file.delete();
    }

    /**
     * Delete the directory and its subdirs associated with the File dir if empty
     * 
     * @param dir
     * @return True if deleted, False else.
     */
    private static boolean deleteRecursiveFileDir(File dir) {
        if (dir == null) {
            return true;
        }
        boolean retour = true;
        if (!dir.exists()) {
            return true;
        }
        File[] list = dir.listFiles();
        if (list == null || list.length == 0) {
            list = null;
            return dir.delete();
        }
        int len = list.length;
        for (int i = 0; i < len; i++) {
            if (list[i].isDirectory()) {
                if (!deleteRecursiveFileDir(list[i])) {
                    retour = false;
                }
            } else {
                retour = false;
                list = null;
                return retour;
            }
        }
        list = null;
        if (retour) {
            retour = dir.delete();
        }
        return retour;
    }
}

Related

  1. deleteRecursive(final File dir)
  2. deleteRecursive(final File file)
  3. deleteRecursive(final File file)
  4. deleteRecursive(final File file)
  5. deleteRecursive(final File file, final boolean collect)
  6. deleteRecursiveDirectories(String mainDir)
  7. deleteRecursiveDirectory(String dirName)
  8. deleteRecursiveFileDir(File dir)
  9. deleteRecursively(File dir)