FileFilter接口
-
public interface FileFilter
- boolean accept (File pathname)
-
当调用File类中的listFiles()方法时,支持传入FileFilter接口接口实现类,对获取文件进行过滤,只有满足条件的文件的才可出现在listFiles()的返回值中
-
package file; import java.io.File; import java.io.FileFilter; import java.util.Date; public class Demo01 { public static void main(String[] args) throws Exception { directoryOpe(); } public static void directoryOpe() throws Exception { File dir2 = new File("e:\\CAD"); System.out.println("========FileFilter接口的使用==========="); File[] files2=dir2.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { if (pathname.getName().endsWith("jpg")) { return true; } return false; } }); for(File file:files2){ System.out.println(file.getName()); } } }//这样只会输出e盘中CAD文件夹中jpg格式的文件