1. 程式人生 > >文件搜索器

文件搜索器

截取 for tools 問題 turn pack ont dex att

 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

文件搜索器