Here you can find the source of deleteDirectory(File dir)
Parameter | Description |
---|---|
dir | the directory to delete |
true
if all deletions were successful; false
otherwise
public static boolean deleteDirectory(File dir)
//package com.java2s; import java.io.*; import java.util.*; public class Main { private static boolean IO_DEBUG = false; /**//ww w.j av a 2s. c om * Deletes dir and all files and subdirectories under dir. (deletes dir also * if dir is a regular file!). * * @param dir the directory to delete * @return <code>true</code> if all deletions were successful; * <code>false</code> otherwise */ public static boolean deleteDirectory(File dir) { return deleteDirectory(dir, null); } /** * Deletes dir and all files and subdirectories under dir (deletes dir also * if dir is a regular file!). * * @param dir the directory to delete * @param excludes files to exclude * @return <code>true</code> if all deletions were successful; * <code>false</code> otherwise */ public static boolean deleteDirectory(File dir, Collection<File> excludes) { // Deletes all files and subdirectories under dir. // Returns true if all deletions were successful. // If a deletion fails, the method stops attempting to delete // and returns false. if (dir.isDirectory()) { String[] children = dir.list(); for (String children1 : children) { boolean success = deleteDirectory(new File(dir, children1), excludes); if (!success) { return false; } } } boolean exclude = false; if (excludes != null) { for (File f : excludes) { if (dir.getAbsolutePath().startsWith(f.getAbsolutePath())) { exclude = true; break; } } } if (excludes == null || !exclude) { // The directory is now empty so delete it boolean result = dir.delete(); if (isDebugginEnabled()) { System.out.println("DELETE: " + result + ", " + dir); } return result; } else { return true; } } /** * @return the debug state */ public static boolean isDebugginEnabled() { return IO_DEBUG; } }