文件名過濾器FilenameFilter的用法
阿新 • • 發佈:2017-07-16
nts div fix bsp oid demo () package 知識
Java.io.FilenameFilter是文件名過濾器,用來過濾不符合規格的文件名,並返回合格的文件;
package cn.test; import java.io.File; import java.io.FilenameFilter; import org.junit.Test; public class Demo4 { @Test public void test1(){ File file = new File("D:/Temp"); 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; public FilterBysuffix(String suffix){ this.suffix=suffix; } @Override public boolean accept(File dir, String name) { return name.endsWith(suffix); } }
結果:
文件名過濾器FilenameFilter的用法