How to find files with specific extension

睁着眼做梦 posted @ 2013年12月03日 11:26 in Java with tags extension java 文档 File 后缀 , 1551 阅读
import java.io.*;
 
public class FindCertainExtension {
 
	private static final String FILE_DIR = "c:\\folder";
	private static final String FILE_TEXT_EXT = ".jpg";
 
	public static void main(String args[]) {
		new FindCertainExtension().listFile(FILE_DIR, FILE_TEXT_EXT);
	}
 
	public void listFile(String folder, String ext) {
 
		GenericExtFilter filter = new GenericExtFilter(ext);
 
		File dir = new File(folder);
 
		if(dir.isDirectory()==false){
			System.out.println("Directory does not exists : " + FILE_DIR);
			return;
		}
 
		// list out all the file name and filter by the extension
		String[] list = dir.list(filter);
 
		if (list.length == 0) {
			System.out.println("no files end with : " + ext);
			return;
		}
 
		for (String file : list) {
			String temp = new StringBuffer(FILE_DIR).append(File.separator)
					.append(file).toString();
			System.out.println("file : " + temp);
		}
	}
 
	// inner class, generic extension filter
	public class GenericExtFilter implements FilenameFilter {
 
		private String ext;
 
		public GenericExtFilter(String ext) {
			this.ext = ext;
		}
 
		public boolean accept(File dir, String name) {
			return (name.endsWith(ext));
		}
	}
}

Ut nonummy habent soluta claritas veniam. Typi nunc soluta hendrerit mutationem sollemnes. Quis lius dolore et insitam vel. Aliquip consequat futurum claram ut mazim. Facilisi accumsan dolore ii imperdiet consequat. Claritatem aliquip quod putamus vulputate iusto. Doming minim typi zzril lius usus. In clari mutationem autem non sit. Qui augue mirum dynamicus gothica ut. Demonstraverunt processus soluta sequitur autem demonstraverunt.

 

先留个底,过两天过来补完

Avatar_small
依云 说:
2013年12月04日 03:57

这么多行代码 -_-|||

Avatar_small
睁着眼做梦 说:
2013年12月04日 10:14

@依云: 网上查到的,呵呵


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter