Here you can find the source of deleteRecursive(File path)
Parameter | Description |
---|---|
path | root path of the directory tree to delete |
public static boolean deleteRecursive(File path)
//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(); } }