Deleting a Directory - Java File Path IO

Java examples for File Path IO:Directory Delete

Description

Deleting a Directory

Demo Code


import java.io.File;

public class Main {
  public static void main(String[] argv) {
    // Delete an empty directory
    boolean success = (new File("directoryName")).delete();
    if (!success) {
      // Deletion failed
    }//from  w  w w  .j a  v  a 2s . c  om
  }
  public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
      String[] children = dir.list();
      for (int i = 0; i < children.length; i++) {
        boolean success = deleteDir(new File(dir, children[i]));
        if (!success) {
          return false;
        }
      }
    }

    // The directory is now empty so delete it
    return dir.delete();
  }
}

Related Tutorials