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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://xxlcube.blog.csdn.net/article/details/8177593
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);
		}
	}

}

展开阅读全文

没有更多推荐了,返回首页