Java遞歸搜索指定文件夾下的匹配文件

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @author tiwson 2010-06-02
 * 
 */
public class FileSearcher {

	/**
	 * 遞歸查找文件
	 * @param baseDirName  查找的文件夾路徑
	 * @param targetFileName  需要查找的文件名
	 * @param fileList  查找到的文件集合
	 */
    public static void findFiles(String baseDirName, String targetFileName, List fileList) {
        /**
         * 算法簡述:
         * 從某個給定的需查找的文件夾出發,搜索該文件夾的所有子文件夾及文件,
         * 若爲文件,則進行匹配,匹配成功則加入結果集,若爲子文件夾,則進隊列。
         * 隊列不空,重複上述操作,隊列爲空,程序結束,返回結果。
         */
        String tempName = null;
        //判斷目錄是否存在
        File baseDir = new File(baseDirName);
        if (!baseDir.exists() || !baseDir.isDirectory()){
            System.out.println("文件查找失敗:" + baseDirName + "不是一個目錄!");
        } else {
        	String[] filelist = baseDir.list();
    	    for (int i = 0; i < filelist.length; i++) {
    	    	File readfile = new File(baseDirName + "\\" + filelist[i]);
    	    	//System.out.println(readfile.getName());
    	        if(!readfile.isDirectory()) {
    	        	tempName =  readfile.getName(); 
                    if (FileSearcher.wildcardMatch(targetFileName, tempName)) {
                        //匹配成功,將文件名添加到結果集
                        fileList.add(readfile.getAbsoluteFile()); 
                    }
    	        } else if(readfile.isDirectory()){
    	        	findFiles(baseDirName + "\\" + filelist[i],targetFileName,fileList);
    	        }
    	    }
        }
    }
    
    /**
     * 通配符匹配
     * @param pattern    通配符模式
     * @param str    待匹配的字符串
     * @return    匹配成功則返回true,否則返回false
     */
    private static boolean wildcardMatch(String pattern, String str) {
        int patternLength = pattern.length();
        int strLength = str.length();
        int strIndex = 0;
        char ch;
        for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {
            ch = pattern.charAt(patternIndex);
            if (ch == '*') {
                //通配符星號*表示可以匹配任意多個字符
                while (strIndex < strLength) {
                    if (wildcardMatch(pattern.substring(patternIndex + 1),
                            str.substring(strIndex))) {
                        return true;
                    }
                    strIndex++;
                }
            } else if (ch == '?') {
                //通配符問號?表示匹配任意一個字符
                strIndex++;
                if (strIndex > strLength) {
                    //表示str中已經沒有字符匹配?了。
                    return false;
                }
            } else {
                if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {
                    return false;
                }
                strIndex++;
            }
        }
        return (strIndex == strLength);
    }

    public static void main(String[] paramert) {
        //    在此目錄中找文件
        String baseDIR = "d:/file"; 
        //    找擴展名爲txt的文件
        String fileName = "*.txt"; 
        List resultList = new ArrayList();
        FileSearcher.findFiles(baseDIR, fileName, resultList); 
        if (resultList.size() == 0) {
            System.out.println("No File Fount.");
        } else {
            for (int i = 0; i < resultList.size(); i++) {
                System.out.println(resultList.get(i));//顯示查找結果。 
            }
        }
    }


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章