Write a Java Program to Delete Empty and Non-empty Directory

Write a Java Program to Delete Empty and Non-empty Directory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.io.File;

class Main {
  public static void main(String[] args) {

    try {
      // create a new file object
      File directory = new File("Directory");

      // delete the directory
      boolean result = directory.delete();

      if(result) {
        System.out.println("Directory Deleted");
      }
      else {
        System.out.println("Directory not Found");
      }

    } catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Final output

Leave a Comment