Empty and delete a folder (and subfolders). : Delete « File Input Output « Java






Empty and delete a folder (and subfolders).

 
import java.io.File;

public class Utils {
  /**
   * Empty and delete a folder (and subfolders).
   * @param folder
   *            folder to empty
   */
  public static void rmdir(final File folder) {
      // check if folder file is a real folder
      if (folder.isDirectory()) {
          File[] list = folder.listFiles();
          if (list != null) {
              for (int i = 0; i < list.length; i++) {
                  File tmpF = list[i];
                  if (tmpF.isDirectory()) {
                      rmdir(tmpF);
                  }
                  tmpF.delete();
              }
          }
          if (!folder.delete()) {
            System.out.println("can't delete folder : " + folder);
          }
      }
  }
}

   
  








Related examples in the same category

1.Deletes all files and subdirectories
2.Remove file or directory
3.Utilities for file delete copy close
4.Deletes the diretory and any files and directories in it recursively.
5.Delete Recursively
6.Delete a file
7.Delete all files under this file and including this file
8.Delete the file or non-empty directory at the supplied path
9.Deletes a directory.
10.Recursively delete a file and all its contents