java檔案受限搜尋

u010660276發表於2013-12-14

搜尋一個目錄下的所有.cpp檔案

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


public class DirFilter implements FilenameFilter{
	public Pattern p;
	public DirFilter(String s){
		p=Pattern.compile(s);
	}
	public boolean accept(File dir,String name){
		return p.matcher(name).matches();
	}

}

import java.io.*;
import java.util.Arrays;
public class cpp {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file=new File("D:\\");
		String [] list ;  
		list=file.list(new DirFilter("\\w*.cpp"));//
		Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);
		System.out.println(list.length);
		for(String s:list)
			System.out.println(s);
	}

}


相關文章