在Java中,可以实现FilenameFilter ,重写accept(File dir,String name)方法,以执行文件过滤功能。
在此示例中,我们向您展示如何使用FilenameFilter
列出文件夹“ c:\\ folder ”中所有以“ .txt ”扩展名结尾的文件 ,然后将其删除。
package com.mkyong.io;
import java.io.*;
public class FileChecker {
private static final String FILE_DIR = "c:\\folder";
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));
}
}
}
翻译自: https://mkyong.com/java/how-to-delete-files-with-certain-extension-only/