?? filefinder.java
字號(hào):
package book.io;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import book.arrayset.Queue;
/**
* 實(shí)現(xiàn)一個(gè)支持通配符的基于廣度優(yōu)先算法的文件查找器
*/
public class FileFinder {
/**
* 查找文件。
* @param baseDirName 待查找的目錄
* @param targetFileName 目標(biāo)文件名,支持通配符形式
* @param count 期望結(jié)果數(shù)目,如果畏0,則表示查找全部。
* @return 滿足查詢條件的文件名列表
*/
public static List findFiles(String baseDirName, String targetFileName, int count) {
/**
* 算法簡(jiǎn)述:
* 從某個(gè)給定的需查找的文件夾出發(fā),搜索該文件夾的所有子文件夾及文件,
* 若為文件,則進(jìn)行匹配,匹配成功則加入結(jié)果集,若為子文件夾,則進(jìn)隊(duì)列。
* 隊(duì)列不空,重復(fù)上述操作,隊(duì)列為空,程序結(jié)束,返回結(jié)果。
*/
List fileList = new ArrayList();
//判斷目錄是否存在
File baseDir = new File(baseDirName);
if (!baseDir.exists() || !baseDir.isDirectory()){
System.out.println("文件查找失敗:" + baseDirName + "不是一個(gè)目錄!");
return fileList;
}
String tempName = null;
//創(chuàng)建一個(gè)隊(duì)列,Queue在第四章有定義
Queue queue = new Queue();//實(shí)例化隊(duì)列
queue.add(baseDir);//入隊(duì)
File tempFile = null;
while (!queue.isEmpty()) {
//從隊(duì)列中取目錄
tempFile = (File) queue.pop();
if (tempFile.exists() && tempFile.isDirectory()) {
File[] files = tempFile.listFiles();
for (int i = 0; i < files.length; i++) {
//如果是目錄則放進(jìn)隊(duì)列
if (files[i].isDirectory()) {
queue.add(files[i]);
} else {
//如果是文件則根據(jù)文件名與目標(biāo)文件名進(jìn)行匹配
tempName = files[i].getName();
if (FileFinder.wildcardMatch(targetFileName, tempName)) {
//匹配成功,將文件名添加到結(jié)果集
fileList.add(files[i].getAbsoluteFile());
//如果已經(jīng)達(dá)到指定的數(shù)目,則退出循環(huán)
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 == '*') {
//通配符星號(hào)*表示可以匹配任意多個(gè)字符
while (strIndex < strLength) {
if (wildcardMatch(pattern.substring(patternIndex + 1),
str.substring(strIndex))) {
return true;
}
strIndex++;
}
} else if (ch == '?') {
//通配符問(wèn)號(hào)?表示匹配任意一個(gè)字符
strIndex++;
if (strIndex > strLength) {
//表示str中已經(jīng)沒(méi)有字符匹配?了。
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";
// 找擴(kuò)展名為txt的文件
String fileName = "*.txt";
// 最多返回5個(gè)文件
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));//顯示查找結(jié)果。
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -