Java Recursive Delete recursiveDelete(String path)

Here you can find the source of recursiveDelete(String path)

Description

Recursively deletes the given path (file or directory).

License

Open Source License

Parameter

Parameter Description
path The root folder to delete.

Declaration

public static void recursiveDelete(String path) 

Method Source Code

//package com.java2s;
/*//from   w ww.  j  av  a  2s.com
 * Copyright (C) 2010-2011 VTT Technical Research Centre of Finland.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Recursively deletes the given path (file or directory).
     *
     * @param path The root folder to delete.
     */
    public static void recursiveDelete(String path) {
        recursiveDelete(new File(path));
    }

    /**
     * Recursively deletes the given path (file or directory).
     *
     * @param file The root folder to delete.
     */
    public static void recursiveDelete(File file) {
        if (!file.exists())
            return;
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                recursiveDelete(f);
            }
        }
        file.delete();
    }

    /**
     * Provide a list of all files of given type in given directory.
     * Type is identified by suffix. For example type "png" gives list of files ending with ".png".
     *
     * @param dir  The directory where to look for the files.
     * @param type The type of files to look for.
     * @param fullPath If true, returns full path of files. Otherwise just the file name.
     * @return The list of files found.
     */
    public static List<String> listFiles(String dir, String type, boolean fullPath) {
        File folder = new File(dir);
        if (!folder.exists())
            throw new IllegalArgumentException("Given dir does not exist:" + dir);
        File[] directoryList = folder.listFiles();
        List<String> files = new ArrayList<>();

        for (File file : directoryList) {
            if (file.isFile()) {
                String name = file.getName();
                if (name.endsWith(type)) {
                    if (fullPath) {
                        files.add(file.getAbsolutePath());
                    } else {
                        files.add(name);
                    }
                }
            } else if (file.isDirectory()) {
                //we ignore directories
            }
        }
        return files;
    }
}

Related

  1. recursiveDelete(final File path, final boolean deleteParent)
  2. recursiveDelete(final File pFile)
  3. recursiveDelete(final File root, final File file)
  4. recursiveDelete(String fileName)
  5. recursiveDelete(String p_path, boolean p_deletemetoo)
  6. recursiveDeleteFile(File f)
  7. recursiveDeleteFile(File file)
  8. recursiveDeleteFile(File file)
  9. recursiveDeleteHelper(File parent, List failed)