Here you can find the source of deleteDir(File fDir)
Parameter | Description |
---|---|
fDir | - directory to be deleted |
public static boolean deleteDir(File fDir)
//package com.java2s; /**/*from w w w . ja v a2 s . com*/ * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved. * * Project: OpenSubsystems * * $Id: FileUtils.java,v 1.12 2007/02/01 07:18:32 * * 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; version 2 of the License. * * 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 */ import java.io.File; public class Main { /** * Deletes all files and sub directories under the specified directory * including the specified directory * * @param strDir - string that specifies directory to be deleted * @return boolean - true if directory was successfully deleted */ public static boolean deleteDir(String strDir) { return ((strDir != null) && (strDir.length() > 0)) ? deleteDir(new File(strDir)) : false; } /** * Deletes all files and sub directories under the specified directory * including the specified directory * * @param fDir - directory to be deleted * @return boolean - true if directory was successfully deleted */ public static boolean deleteDir(File fDir) { boolean bRetval = false; if (fDir != null && fDir.exists()) { bRetval = deleteDirectoryContent(fDir); if (bRetval) { bRetval = bRetval && fDir.delete(); } } return bRetval; } /** * Delete all files and directories in directory but do not delete the * directory itself. * * @param strDir - string that specifies directory to delete * @return boolean - success flag */ public static boolean deleteDirectoryContent(String strDir) { return ((strDir != null) && (strDir.length() > 0)) ? deleteDirectoryContent(new File(strDir)) : false; } /** * Delete all files and directories in directory but do not delete the * directory itself. * * @param fDir - directory to delete * @return boolean - success flag */ public static boolean deleteDirectoryContent(File fDir) { boolean bRetval = false; if (fDir != null && fDir.isDirectory()) { File[] files = fDir.listFiles(); if (files != null) { bRetval = true; boolean dirDeleted; for (int index = 0; index < files.length; index++) { if (files[index].isDirectory()) { // TODO: Performance: Implement this as a queue where // you add to the end and take from the beginning, it will be more // efficient than the recursion dirDeleted = deleteDirectoryContent(files[index]); if (dirDeleted) { bRetval = bRetval && files[index].delete(); } else { bRetval = false; } } else { bRetval = bRetval && files[index].delete(); } } } } return bRetval; } }