Java File Path Delete deleteDirectory(File path)

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

Description

Delete recursively a directory.

License

Open Source License

Parameter

Parameter Description
path path to the directory (or file) you want to delete

Return

true if the delete was successfull and false if the delete was unable to erase all the directory

Declaration

static public boolean deleteDirectory(File path) 

Method Source Code


//package com.java2s;
/*//from  w  ww  . jav  a  2 s  .c  o m
  This file is part of JOrigin Common Library.
    
JOrigin Common 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.
    
JOrigin Common 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 JOrigin Common.  If not, see <http://www.gnu.org/licenses/>.
    
*/

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

public class Main {
    /**
     * Delete recursively a directory. 
     * @param path path to the directory (or file) you want to delete
     * @return <code>true</code> if the delete was successfull and <code>false</code> if the delete was unable to erase all the directory
     */
    static public boolean deleteDirectory(File path) {
        boolean result = true;
        // Verification de l'existence du repertoire
        if (path.exists()) {
            // Liste des fichiers (et sous repertoire)
            File[] files = path.listFiles();
            // si on a un fichier simple, on l'efface
            if (files == null) {
                return path.delete();
            }
            for (int i = 0; i < files.length; i++) {
                // Utilisation de la recursivite pour l'effacage
                if (files[i].isDirectory()) {
                    result &= !deleteDirectory(files[i]);
                }
                result &= files[i].delete();
            }
        }
        // effacage du repertoire lui meme
        result &= path.delete();
        return (result);
    }

    /**
     * List recursively a directory and its sub-directories.
     * @param source the source directory (or file to copy).
     * @param filter a {@link java.io.FileFilter filter} used for accepting files within the list.
     * @return the list of files presents in the directory.
     */
    public static ArrayList<File> listFiles(File source, FileFilter filter) {
        ArrayList<File> list = null;
        // Premiere verification: le source existe t'il
        if (!source.exists()) {
            return null;
        } else {
            list = new ArrayList<File>();
        }
        // Liste les fichiers du repertoire
        File[] files = source.listFiles();
        // Parcours de la liste de fichiers et copie recursive
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                list.addAll(listFiles(files[i], filter));
            } else {
                if ((filter == null) || (filter.accept(files[i]))) {
                    list.add(files[i]);
                }
            }
        }
        return list;
    }
}

Related

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