|
吾爱游客
发表于 2024-11-21 10:26
申请会员ID:DTG
个人邮箱:dutian312@gmail.com
技术能力:c#, java, python
贴一个之前写的文件匹配器算法
实现功能: 通过指定模糊路径进行搜索符合的文件,并返回文件集合,支持单层/多层目录
例如:
d://下载/*/*.jpg
d://下载/**/*ncs*.jpg
d://**/1w*ooo.*jp*
核心算法:
public static List<String> findPath(String matchPath) {
List<List<String>> tarPaths = new ArrayList();
String[] pathSp = matchPath.split("/");
for(int i = 0; i < pathSp.length; ++i) {
List<String> curPath = new ArrayList();
if (i == 0) {
curPath.add(pathSp);
} else {
List<String> befotePath = (List)tarPaths.get(i - 1);
File file = null;
Iterator var7;
String string;
String fileName;
if (pathSp.contains("**")) {
if (pathSp.length - 1 != i) {
String fileFilter = pathSp[pathSp.length - 1];
Iterator var18 = befotePath.iterator();
while(var18.hasNext()) {
fileName = (String)var18.next();
file = new File(fileName);
eachFileList(file, curPath, fileFilter);
}
return curPath;
}
var7 = befotePath.iterator();
while(var7.hasNext()) {
string = (String)var7.next();
file = new File(string);
eachFileList(file, curPath);
}
} else if (!pathSp.contains("*")) {
var7 = befotePath.iterator();
while(var7.hasNext()) {
string = (String)var7.next();
curPath.add(string + "/" + pathSp);
}
} else {
var7 = befotePath.iterator();
while(var7.hasNext()) {
string = (String)var7.next();
file = new File(string);
try {
int var11;
if (pathSp.trim().equals("*")) {
if (file.listFiles() != null) {
File[] var19 = file.listFiles();
int var20 = var19.length;
for(var11 = 0; var11 < var20; ++var11) {
File files = var19[var11];
curPath.add(string + "/" + files.getName());
}
}
} else {
fileName = pathSp;
if (file.listFiles() != null) {
File[] var10 = file.listFiles();
var11 = var10.length;
for(int var12 = 0; var12 < var11; ++var12) {
File files = var10[var12];
String fname = files.getName();
Boolean boo = StringHelper.strMatchSign(fname, fileName);
if (boo) {
curPath.add(string + "/" + files.getName());
}
}
}
}
} catch (Exception var16) {
HandleException.addEx(var16);
}
}
}
}
tarPaths.add(curPath);
}
return (List)tarPaths.get(tarPaths.size() - 1);
}
private static void eachFileList(File file, List<String> curPath) {
if (file.exists() && file.listFiles() != null) {
File[] var2 = file.listFiles();
int var3 = var2.length;
for(int var4 = 0; var4 < var3; ++var4) {
File file_01 = var2[var4];
if (file_01.exists()) {
String path = file_01.getParent() + "\\" + file_01.getName();
curPath.add(path.replace("\\", "/"));
eachFileList(file_01, curPath);
}
}
}
}
private static void eachFileList(File file, List<String> curPath, String fileFilter) {
if (file.exists() && file.listFiles() != null) {
File[] var3 = file.listFiles();
int var4 = var3.length;
for(int var5 = 0; var5 < var4; ++var5) {
File file_01 = var3[var5];
if (file_01.exists()) {
if (StringHelper.strMatchSign(file_01.getName(), fileFilter)) {
String path = file_01.getParent() + "\\" + file_01.getName();
curPath.add(path.replace("\\", "/"));
}
eachFileList(file_01, curPath, fileFilter);
}
}
}
}
|
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|