Java Delete File Recursively deleteRecursiveFileDir(File dir)

Here you can find the source of deleteRecursiveFileDir(File dir)

Description

Delete the directory and its subdirs associated with the File dir if empty

License

Open Source License

Parameter

Parameter Description
dir a parameter

Return

True if deleted, False else.

Declaration

private static boolean deleteRecursiveFileDir(File dir) 

Method Source Code

//package com.java2s;
/**//  ww  w. j a  v a2  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 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;
    }

    /**
     * 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();
    }
}

Related

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