文件搜索器

 1 package com.arraySet;
 2 
 3 import java.util.LinkedList;
 4 
 5 public class Queue {
 6     private LinkedList data = new LinkedList();
 7     
 8     public Queue(){}
 9     
10     public boolean isEmpty(){
11         return data.isEmpty();
12     }
13     
14     //往队列尾部添加对象
15     public void add(Object e){
16         this.data.addLast(e);
17     }
18     //查看队列首个对象
19     public Object peek(){
20         if(this.isEmpty()){
21             return null;
22         }
23         return this.data.getFirst();
24     }
25     
26     //移除队列首个对象
27     public boolean remove(){
28         if(this.isEmpty()){
29             return false;
30         }
31         data.removeFirst();
32         return true;
33     }
34     
35     //弹出首个对象
36     public Object pop(){
37         if(this.isEmpty()){
38             return null;
39         }
40         return data.removeFirst();
41     }
42     
43     //查询任意对象在队列中的索引
44     public int index(Object e){
45         if(this.isEmpty()){
46             return -1;
47         }
48         return data.indexOf(e);
49     }
50     
51     //清空队列
52     public void clear(){
53         data.clear();
54     }
55 }
  1 package com.test;
  2 
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 import com.arraySet.Queue;
  8 
  9 /**
 10  * 查找给定的路径baseDirName下匹配符合targetFileName格式的count个文件
 11  * @author Administrator
 12  *
 13  */
 14 public class FileFinder {
 15     public static List findfiles(String baseDirName,String targetFileName,int count){
 16         List fileList = new ArrayList();
 17         File baseDir = new File(baseDirName);
 18         
 19         if(!baseDir.exists() || !baseDir.isDirectory()){
 20             System.out.println("文件查找失败:"+baseDirName+"不存在或者不是目录!");
 21             return fileList;
 22         }
 23         Queue queue = new Queue();
 24         //用于临时保存队列文件夹中的文件名
 25         String tempName = null;
 26         queue.add(baseDir);
 27         //外层循环
 28         while(!queue.isEmpty()){
 29             File tempFile = (File)queue.pop();
 30             if(tempFile.exists() && tempFile.isDirectory()){
 31                 File[] files = tempFile.listFiles();
 32                 for(File file:files){
 33                     if(file.isDirectory()){
 34                         queue.add(file);
 35                     }else{
 36                         tempName = file.getName();
 37                         //判断文件名与表达式是否匹配
 38                         if(FileFinder.wildcardMatch(targetFileName, tempName)){
 39                             fileList.add(file.getAbsolutePath());
 40                             if((count!=0) && fileList.size()>=count){
 41                                 //退出整个外部循环
 42                                 return fileList;
 43                             }
 44                         }
 45                     }
 46                 }
 47             }
 48         }
 49         return fileList;
 50     }
 51     
 52     /**
 53      * 遗留问题:在类方法中的全局变量patternLength、strLength、tempLength,在嵌套循环中被调用,方法中的全局如何分别存储方法加载的值和嵌套循环的值
 54      * @param pattern
 55      * @param str
 56      * @return
 57      */
 58     private static boolean wildcardMatch(String pattern, String str){
 59 //        System.out.print("pattern:"+pattern+"\t"+"str:"+str+"\t");
 60         int patternLength = pattern.length();
 61         int strLength = str.length();
 62         int tempLength = 0;
 63         char ch;
 64         for(int patternIndex=0;patternIndex < patternLength;patternIndex++){
 65             ch = pattern.charAt(patternIndex);
 66             if(ch=='*'){
 67                 //*后面的字符串直到匹配到最后找到匹配为止,截取字符串从下标0开始,而字符串长度从1开始
 68                 while(tempLength < (strLength-1)){
 69 //                    System.out.println("tempLength1:"+tempLength);
 70                     //调用的时候pattern、str值为截取后的字符串,调用结束返回时恢复为方法加载的全局值
 71                     if(wildcardMatch(pattern.substring(patternIndex + 1), str.substring(tempLength))){
 72                         return true;
 73                     }
 74                     tempLength++;
 75 //                    System.out.println("tempLength2:"+tempLength);
 76                 }
 77             }else if(ch=='?'){
 78                 tempLength++;
 79                 if(tempLength>strLength){
 80                     return false;
 81                 }
 82             }else{
 83                 if((tempLength>strLength) || (ch!=str.charAt(tempLength))){
 84                     return false;
 85                 }
 86                 tempLength++;
 87             }
 88         }
 89         return (tempLength == strLength);
 90     }
 91     
 92     private static void printArrayList(List list){
 93         for(int i=0;i<list.size();i++){
 94             System.out.println(list.get(i));
 95         }
 96     }
 97     public static void main(String[] args) {    
 98         String baseDIR = "D:/software/jdk/jdk7/lib";
 99         String targetFileName1 = "*.jar";
100         List fileList = FileFinder.findfiles(baseDIR, targetFileName1, 8);
101         FileFinder.printArrayList(fileList);
102     }
103 }
1 执行结果:
2 D:\software\jdk\jdk7\lib\ant-javafx.jar
3 D:\software\jdk\jdk7\lib\dt.jar
4 D:\software\jdk\jdk7\lib\javafx-doclet.jar
5 D:\software\jdk\jdk7\lib\javafx-mx.jar
6 D:\software\jdk\jdk7\lib\jconsole.jar
7 D:\software\jdk\jdk7\lib\sa-jdi.jar
8 D:\software\jdk\jdk7\lib\tools.jar
9 D:\software\jdk\jdk7\lib\missioncontrol\mc.jar

猜你喜欢

转载自www.cnblogs.com/celine/p/8996894.html
今日推荐