日期:2014-05-20 浏览次数:20773 次
import java.io.File; import java.io.FilenameFilter; import java.util.regex.*; import java.util.*; public class DirList { public static void main(String[] args) { String filePath="d:\\"; String filterName=".java"; FilenameFilter ff = new DirFilter(filterName); File f = new File(filePath); String[] list = f.list(ff); Arrays.sort(list,String.CASE_INSENSITIVE_ORDER); for(String dirItem : list){ System.out.println(dirItem); } } static class DirFilter implements FilenameFilter{ private Pattern pattern; public DirFilter(String regex){ pattern = Pattern.compile(regex); } public boolean accept(File dirFile , String name){ return pattern.matcher(name).matches(); } } }