📄 filefinder.java
字号:
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* 实现一个支持通配符的基于广度优先的文件查询器
*/
public class FileFinder {
/**
* 查找文件 baseDirName 待查找的目录 targetFileName 目标文件名,支持通配符形式 return 满足查询条件的文件列表
*/
public static List<File> findFiles(String baseDirName, String targetFileName) {
/*
* 算法简介 从某个给定的需查找的文件夹出发,搜索该文件夹以及子文件夹 若为文件 则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列
* 队列不为空,重复上面操作
*/
// List<File> fileList = new ArrayList<File>();
List<File> fileList = new ArrayList<File>();
File baseDir = new File(baseDirName);
if (baseDirName.endsWith(File.separator)) {
baseDirName = baseDirName + File.separator;
}
if (!baseDir.exists() || !baseDir.isDirectory()) {
System.out.println("文件查找失败:" + baseDirName + "不存在或不是一个目录");
return fileList;
}
String tempName = null;
Queue queue = new Queue();
queue.add(baseDir);
File tempFile = null;
// System.out.println("baseDir is "+ baseDir.getAbsolutePath());
try {
while (!queue.isEmpty()) {
tempFile = (File) queue.pop();
System.out.println("弹出的tempFile is "
+ tempFile.getAbsolutePath());
if (tempFile.exists() && tempFile.isDirectory()) {
File[] files = tempFile.listFiles();
try {
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
System.out.println("压入:" + files[i]);
queue.add(files[i]);
} else {
tempName = files[i].getName();
System.out.println(tempName);
if (FileFinder.wildcardMatch(targetFileName,
tempName)) {
fileList.add(files[i].getAbsoluteFile());
}
}
}
}catch (NullPointerException e) {
e.printStackTrace();
}
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
return fileList;
}
/*
* 通配符匹配 patten 通配符模式 str 待匹配的字符串 return 匹配成功则返回true,否则返回false
*/
public static boolean wildcardMatch(String patten, String str) {
int pattenLength = patten.length();
int strLength = str.length();
int strIndex = 0;
char ch;
for (int pattenIndex = 0; pattenIndex < pattenLength; pattenIndex++) {
ch = patten.charAt(pattenIndex);
if (ch == '*') {
// 通配符*可以匹配任意多个字符
while (strIndex < strLength) {
if (wildcardMatch(patten.substring(pattenIndex + 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[] args) {
String baseDir = "F:";// 在此目录中查找文件
String fileName = "*.mp3";// 找扩展名为txt的文件
List<File> resultList = FileFinder.findFiles(baseDir, fileName);
if (resultList.size() == 0) {
System.out.println("没有找到文件");
} else {
for (int i = 0; i < resultList.size(); i++) {
System.out.println(resultList.get(i));
}
}
/*
* findFiles("E:","*.txt"); String dir="E:"; File file=new File(dir);
* try { System.out.println(file.getCanonicalPath()); } catch(Exception
* e) { }
*/
}
}
class Queue {
private LinkedList<Object> data = new LinkedList<Object>();
public Queue() {
}
/* 向队列中添加一元素,只能加到队尾 */
public void add(Object obj) {
this.data.addLast(obj);
}
/* 判断队列是否为空 */
public boolean isEmpty() {
return data.isEmpty();
}
/* 弹出元素,即获取队列首元素并将其从队列擅除 */
public Object pop() {
if (data.isEmpty()) {
System.out.println("队列为空!");
return null;
}
return data.removeFirst();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -