- 注册时间
- 2004-8-29
- 最后登录
- 1970-1-1
|
发表于 2006-12-8 22:52:08
|
显示全部楼层
来个JAVA版本的吧,支持正则式:- import java.io.*;
- import java.util.regex.*;
- /**
- *@author Eastsun
- *@version 1.0 12/8/2006
- */
- public class SearchFile{
- /**
- *@param dir 搜索进行的根目录
- *@param p 文件名匹配的模式
- *@param out 负责输出搜索结果PrintStream
- */
- public static int searchFile(File dir,Pattern p,PrintStream out)throws IOException{
- int count =0;
- if(dir.isDirectory()){
- File[] list =dir.listFiles();
- if(list==null) return 0;
- for(int n=0;n<list.length;n++){
- if(list[n].isDirectory()) count +=searchFile(list[n],p,out);
- else if(p.matcher(list[n].getName()).matches()){
- out.println(list[n].getCanonicalPath());
- count++;
- }
- }
- }
- return count;
- }
- public static void main(String[] args)throws IOException{
- File root =null;
- Pattern p =null;
- PrintStream out =null;
-
- if(args.length==3){
- root =new File(args[0]); //搜索的根目录
- p =Pattern.compile(args[1]); //匹配模式
- out =new PrintStream(new File(args[2])); //将搜索结果保存到文件名为args[2]的文件中
- }
- else{
- root =new File("e:\"); //搜索的根目录为e:盘
- p = Pattern.compile(".*\\.lav"); //搜索所有的lav文件
- out =System.out; //直接在控制台上显示搜索结果
- }
- System.out.println("Searching..");
- int count =searchFile(root,p,out);
- System.out.println("Find "+count+" files");
- }
- }
复制代码 |
|