获取某一路径下指定后缀名的文件

package file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;

class DirFilter implements FilenameFilter {//文件过滤器
	private Pattern p;
	public DirFilter(String regex) {
		p = Pattern.compile(regex);
	}
	@Override
	public boolean accept(File dir, String name) {
		// TODO Auto-generated method stub
		return p.matcher(name).matches();
	}
}

public class FileDemo{
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file = new File("E:/Intern Record/2012-7");
		String str[] = file.list(new DirFilter(".*(.pdf)$"));//正则
		System.out.println(str.length);
		for(String d:str) {
			System.out.println(d);
		}
	}

}

你可能感兴趣的