Delete Recursively : Delete « File Input Output « Java






Delete Recursively

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

public class Utils {
  private static void deleteContentsRecursive(File file) throws IOException {
    File[] files = file.listFiles();
    for (File child : files) {
      if (child.isDirectory())
        deleteContentsRecursive(child);
      if (!child.delete())
        throw new IOException("Unable to delete " + child.getPath());
    }
  }

  public static void deleteRecursive(File dir) throws IOException {
    if (dir.isDirectory())
      deleteContentsRecursive(dir);
    if (!dir.delete())
      throw new IOException("Unable to delete " + dir);
  }

}

   
  








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 a file
6.Delete all files under this file and including this file
7.Delete the file or non-empty directory at the supplied path
8.Deletes a directory.
9.Recursively delete a file and all its contents
10.Empty and delete a folder (and subfolders).