package com.zh.test; import java.io.File; import java.io.FilenameFilter; public class Test2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub File f = new File("D:/java"); //过滤以“Ex”开头的文件和目录,存放到字符串数组s中。 String s[] = f.list(new JavaFilter("Ex")); for(int i=0;i<s.length;i++){ File ff = new File("D:/java/"+s[i]); if(ff.isDirectory()){ System.out.println(s[i]+" is a directory"); }else{ System.out.println(s[i]+" is a file"); } } } } //FilenameFilter 过滤器文件名 class JavaFilter implements FilenameFilter{ String jf; public JavaFilter(String s){ jf = s; } //测试指定文件是否应该包含在某一文件列表中 public boolean accept(File dir, String name) { // TODO Auto-generated method stub return name.startsWith(jf); } }