Deletes a directory. : Delete « File « Java Tutorial






import java.io.File;
import java.io.IOException;

public class Utils {
  /**
   * Deletes a directory.
   *
   * @param dir the file for the directory to delete
   * @return true if susscess
   */
  public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
      for (File file : dir.listFiles()) {
        if (file.isDirectory()) {
          try {
            if (file.getCanonicalFile().getParentFile().equals(dir.getCanonicalFile())) {
              deleteDir(file);
              if (file.exists() && !file.delete()) {
                System.out.println("Can't delete: " + file);
              }
            } else {
              System.out.println("Warning: " + file + " may be a symlink.  Ignoring.");
            }
          } catch (IOException e) {
            System.out.println("Warning: Cannot determine canonical file for " + file + " - ignoring.");
          }
        } else {
          if (file.exists() && !file.delete()) {
            System.out.println("Can't delete: " + file);
          }
        }
      }
      return dir.delete();
    }
    return false;
  }
}








11.79.Delete
11.79.1.Deletes the diretory and any files and directories in it recursively.
11.79.2.Delete the file or non-empty directory at the supplied path
11.79.3.Delete all files under this file and including this file
11.79.4.Deletes a directory.
11.79.5.Empty and delete a folder (and subfolders).
11.79.6.Recursive directory deletion
11.79.7.Recursively delete a file and all its contents
11.79.8.Recursivly delete directory
11.79.9.Remove file or directory
11.79.10.Remove a directory and all of its contents.
11.79.11.Deletes all files and subdirectories