關(guān)鍵技術(shù):
- 采用廣度優(yōu)先查找算法,即先搜索當(dāng)前目錄下的文件,在搜索子目錄下的文件。
- 使用隊列Queue存放所有還沒處理的子目錄,當(dāng)隊列為空時,搜索完畢。
- 根據(jù)文件名匹配搜索條件的模式,目錄名不參與匹配。
package book.io;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import book.arrayset.Queue;
/**
* 實現(xiàn)一個支持通配符的基于廣度優(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) {
/**
* 算法簡述:
* 從某個給定的需查找的文件夾出發(fā),搜索該文件夾的所有子文件夾及文件,
* 若為文件,則進(jìn)行匹配,匹配成功則加入結(jié)果集,若為子文件夾,則進(jìn)隊列。
* 隊列不空,重復(fù)上述操作,隊列為空,程序結(jié)束,返回結(jié)果。
*/
List fileList = new ArrayList();
//判斷目錄是否存在
File baseDir = new File(baseDirName);
if (!baseDir.exists() || !baseDir.isDirectory()){
System.out.println("文件查找失敗:" + baseDirName + "不是一個目錄!");
return fileList;
}
String tempName = null;
//創(chuàng)建一個隊列,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++) {
//如果是目錄則放進(jìn)隊列
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 == '*') {
//通配符星號*表示可以匹配任意多個字符
while (strIndex < strLength) {
if (wildcardMatch(pattern.substring(patternIndex + 1),
str.substring(strIndex))) {
return true;
}
strIndex++;
}
} else if (ch == '?') {
//通配符問號?表示匹配任意一個字符
strIndex++;
if (strIndex > strLength) {
//表示str中已經(jīng)沒有字符匹配?了。
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個文件
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é)果。
}
}
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import book.arrayset.Queue;
/**
* 實現(xiàn)一個支持通配符的基于廣度優(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) {
/**
* 算法簡述:
* 從某個給定的需查找的文件夾出發(fā),搜索該文件夾的所有子文件夾及文件,
* 若為文件,則進(jìn)行匹配,匹配成功則加入結(jié)果集,若為子文件夾,則進(jìn)隊列。
* 隊列不空,重復(fù)上述操作,隊列為空,程序結(jié)束,返回結(jié)果。
*/
List fileList = new ArrayList();
//判斷目錄是否存在
File baseDir = new File(baseDirName);
if (!baseDir.exists() || !baseDir.isDirectory()){
System.out.println("文件查找失敗:" + baseDirName + "不是一個目錄!");
return fileList;
}
String tempName = null;
//創(chuàng)建一個隊列,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++) {
//如果是目錄則放進(jìn)隊列
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 == '*') {
//通配符星號*表示可以匹配任意多個字符
while (strIndex < strLength) {
if (wildcardMatch(pattern.substring(patternIndex + 1),
str.substring(strIndex))) {
return true;
}
strIndex++;
}
} else if (ch == '?') {
//通配符問號?表示匹配任意一個字符
strIndex++;
if (strIndex > strLength) {
//表示str中已經(jīng)沒有字符匹配?了。
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個文件
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é)果。
}
}
}
}
-- 學(xué)海無涯