Java File Path Delete deleteRecursive(File path)

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

Description

Remove a file/directory.

License

Open Source License

Parameter

Parameter Description
path root path of the directory tree to delete

Return

true on success, false otherwise

Declaration

public static boolean deleteRecursive(File path) 

Method Source Code

//package com.java2s;
/*/*from  w  w w .ja  v  a  2s . c om*/
 * Copyright (C) 2015 Dieter J Kybelksties
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @date: 2015-12-16
 * @author: Dieter J Kybelksties
 */

import java.io.File;

public class Main {
    /**
     * Remove a file/directory. In case of directory recursively remove all
     * contained files and directories.
     *
     * @param path root path of the directory tree to delete
     * @return true on success, false otherwise
     */
    public static boolean deleteRecursive(File path) {
        boolean deleteSuccessful = false;
        if (!exists(path)) {
            return deleteSuccessful;
        }
        boolean ret = true;
        if (path.isDirectory()) {
            for (File f : path.listFiles()) {
                ret = ret && deleteRecursive(f);
            }
            deleteSuccessful = path.delete();
        } else {
            deleteSuccessful = path.delete();
        }
        return ret && deleteSuccessful;
    }

    /**
     * Recursively delete all files/directories in the path-string.
     *
     * @param pathString root path of the directory tree to delete as string
     * @return true on success, false otherwise
     */
    public static boolean deleteRecursive(String pathString) {
        return deleteRecursive(new File(pathString == null ? "" : pathString));
    }

    /**
     * Check whether a path (file or directory) exists.
     *
     * @param path the whole path
     * @return true, if the path exists, false otherwise
     */
    public static boolean exists(File path) {
        return path != null && path.exists();
    }

    /**
     * Check whether a path (file or directory) exists.
     *
     * @param pathString the whole path as string
     * @return true, if the path exists, false otherwise
     */
    public static boolean exists(String pathString) {
        if (pathString == null) {
            return false;
        }
        return new File(pathString).exists();
    }
}

Related

  1. deleteQuietly(Object path)
  2. deleteRecursive(File path)
  3. deleteRecursive(File path)
  4. deleteRecursive(File path)
  5. deleteRecursive(File path)
  6. deleteRecursive(File path)
  7. deleteRecursive(File path, boolean debug)
  8. deleteRecursively(File path)
  9. deleteRecursively(File path, File parentCanonical)