Xóa thư mục trong java

Trước đó tôi đã hướng dẫn làm thế nào để xóa một fileđổi tên file trong java .Ở bài viết này, chúng ta sẽ tìm hiểu làm thế nào để xóa các tập tin trong một thư mục. java.io.File cung cấp các phương thức ofdir.isDirectory(),dir.list() and deleteDir() .

Ví dụ về Xóa thư mục trong  java:

 

import java.io.File;

public class Main {
   public static void main(String[] argv) throws Exception {
      deleteDir(new File("c:\\tempFoder"));
   }
   public static boolean deleteDir(File dirFoder) {
      if (dirFoder.isDirectory()) {
         String[] children = dirForder.list();
         for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir (new File(dirFoder, children[i]));
            
            if (!success) {
               return false;
            }
         }
      }
      return dir.delete();
      System.out.println("Thư mục đã bị xóa");
   }
}

 

Kết quả

Thư mục đã bị xóa
x