Posted on 2009-05-24 21:13
啥都寫點 閱讀(405)
評論(0) 編輯 收藏 所屬分類:
J2SE
關鍵技術:
- 采用廣度優先查找算法,即先搜索當前目錄下的文件,在搜索子目錄下的文件。
- 使用隊列Queue存放所有還沒處理的子目錄,當隊列為空時,搜索完畢。
- 根據文件名匹配搜索條件的模式,目錄名不參與匹配。
package book.io;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import book.arrayset.Queue;
/**
* 實現一個支持通配符的基于廣度優先算法的文件查找器
*/
public class FileFinder {
/**
* 查找文件。
* @param baseDirName 待查找的目錄
* @param targetFileName 目標文件名,支持通配符形式
* @param count 期望結果數目,如果畏0,則表示查找全部。
* @return 滿足查詢條件的文件名列表
*/
public static List findFiles(String baseDirName, String targetFileName, int count) {
/**
* 算法簡述:
* 從某個給定的需查找的文件夾出發,搜索該文件夾的所有子文件夾及文件,
* 若為文件,則進行匹配,匹配成功則加入結果集,若為子文件夾,則進隊列。
* 隊列不空,重復上述操作,隊列為空,程序結束,返回結果。
*/
List fileList = new ArrayList();
//判斷目錄是否存在
File baseDir = new File(baseDirName);
if (!baseDir.exists() || !baseDir.isDirectory()){
System.out.println("文件查找失敗:" + baseDirName + "不是一個目錄!");
return fileList;
}
String tempName = null;
//創建一個隊列,Queue在第四章有定義
Queue queue = new Queue();//實例化隊列
queue.add(baseDir);//入隊
File tempFile = null;
while (!queue.isEmpty()) {
//從隊列中取目錄
tempFile = (File) queue.pop();
if (tempFile.exists() && tempFile.isDirectory()) {
File[] files = tempFile.listFiles();
for (int i = 0; i < files.length; i++) {
//如果是目錄則放進隊列
if (files[i].isDirectory()) {
queue.add(files[i]);
} else {
//如果是文件則根據文件名與目標文件名進行匹配
tempName = files[i].getName();
if (FileFinder.wildcardMatch(targetFileName, tempName)) {
//匹配成功,將文件名添加到結果集
fileList.add(files[i].getAbsoluteFile());
//如果已經達到指定的數目,則退出循環
if ((count != 0) && (fileList.size() >= count)) {
return fileList;
}
}
}
}
}
}
return 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 = "C:/temp";
// 找擴展名為txt的文件
String fileName = "*.txt";
// 最多返回5個文件
int countNumber = 5;
List resultList = FileFinder.findFiles(baseDIR, fileName, countNumber);
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));//顯示查找結果。
}
}
}
}
--
學海無涯