Xóa nhiều file trong Java với phần mở rộng nhất định

Trong bài viết này, tôi sẽ hướng dẫn các bạn cách Xóa nhiều file trong Java cùng một lúc với phần mở rộng nhất định. Nghĩa là xóa các file đã biết trước đuôi file là gì, ví dụ .dat, .txt…

Giải pháp:

Trong Java, bạn có thể thực hiện các FilenameFilter ,phương thức ghi đè các accept (File dir, String name) , để thực hiện chức năng lọc các file trong Java.

Trong ví dụ này, tôi chỉ sẽ hướng dẫn các bạn cách sử dụng FilenameFilterđể liệt kê tất cả các file kết thúc có phần mở rộng là “.txt ” trong thư mục ” C:\\deletefile“, sau đó xóa nó các file có đuổi .txt trong thư mục đó.

Tạo file FileChecker như sau:

package com.itphutran.file;

import java.io.*;

public class FileChecker {

   private static final String FILE_DIR = "E:\\project\\huongdanjava\\javaio\\deletefile";
   private static final String FILE_TEXT_EXT = ".txt";

   public static void main(String args[]) {
	   new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT);
   }

   public void deleteFile(String folder, String ext){

     GenericExtFilter filter = new GenericExtFilter(ext);
     File dir = new File(folder);

     //list out all the file name with .txt extension
     String[] list = dir.list(filter);

     if (list.length == 0) return;

     File fileDelete;

     for (String file : list){
   	String temp = new StringBuffer(FILE_DIR)
                      .append(File.separator)
                      .append(file).toString();
    	fileDelete = new File(temp);
    	boolean isdeleted = fileDelete.delete();
    	System.out.println("file : " + temp + " is deleted : " + isdeleted);
     }
   }

   //inner class, generic extension filter
   public class GenericExtFilter implements FilenameFilter {

       private String ext;

       public GenericExtFilter(String ext) {
         this.ext = ext;
       }

       public boolean accept(File dir, String name) {
         return (name.endsWith(ext));
       }
    }
}

Đầu tiên:

Xóa nhiều file trong Java

Sau khi thực hiện xóa:

file : E:\project\huongdanjava\javaio\deletefile\filename.txt is deleted : true
file : E:\project\huongdanjava\javaio\deletefile\newFile.txt is deleted : true
file : E:\project\huongdanjava\javaio\deletefile\newFile2.txt is deleted : true
file : E:\project\huongdanjava\javaio\deletefile\newFile3.txt is deleted : true
file : E:\project\huongdanjava\javaio\deletefile\newFile4.txt is deleted : true
file : E:\project\huongdanjava\javaio\deletefile\newFileWrite.txt is deleted : true
file : E:\project\huongdanjava\javaio\deletefile\testFile.txt is deleted : true
file : E:\project\huongdanjava\javaio\deletefile\writeFile.txt is deleted : true

Xóa nhiều file trong Java

#Tổng kết:

Như vậy, các ban thấy rằng chúng ta đã xóa nhiều file thành công! Quá trình xóa nhiều  file trong java xử lý có phức tạp tuy nhiên chúng ta biết cách sử dụng thì nó đơn giản đúng ko các bạn?

Các bạn cần ôn và  thực hiện cho tốt và biết cách xử lý. Vì phần file là một trong những phần thường xuyên sử dụng trong các ứng dụng.

Chúc các bạn thực hiện thành công!

0 0 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Phản hồi nội tuyến
Xem tất cả bình luận
x