⽂件名过滤器FilenameFilter的⽤法.io.FilenameFilter是⽂件名过滤器,⽤来过滤不符合规格的⽂件名,并返回合格的⽂件;
实例1,匹配指定字符结尾的⽂件
st;
import java.io.File;headmaster
import java.io.FilenameFilter;
import org.junit.Test;
public class Demo4 {
@Test
初次见面说什么public void test1(){
File file = new File("D:/Temp");
frenchcum
FilterBysuffix filter = new FilterBysuffix(".txt");
String[] files = file.list(filter);
for (String fileName : files){
System.out.println(fileName);
}
}
}
class FilterBysuffix implements FilenameFilter{
private String suffix;
millionairepublic FilterBysuffix(String suffix){
this.suffix=suffix;
december缩写}
@Override
public boolean accept(File dir, String name) {
dsWith(suffix);
}
comodo是什么意思
}
结果:
实例2,正则匹配⽂件
public class Test1 {
@Test
public void demo1() throws IOException{
File file = new File("D:/tmp");
FilterByMatches filterByMatches = new FilterByMatches(".*txt$");
String[] files = file.list(filterByMatches);
中国新年习俗for(String f : files){
System.out.println(f);
}
深大师院国际高中}
}
class FilterByMatches implements FilenameFilter{pcs
private String suffix;
public FilterByMatches(String suffix){
this.suffix=suffix;
}
public boolean accept(File dir, String name) {
return name.matches(suffix);
英文小说读后感
}
}
结果: