package com.zhongxin;
import java.io.File;
/**
* @ClassName Test11
* @Description TODO
* @Author houbing
* @Date 2023/9/4 10:12
*/
public class Test11 {
public static void main(String[] args) {
String folderPath = "D:\\Galaxy\\upload\\20230901"; // 文件夹路径
String str = ".*zh049.*.xls"; // 要搜索的文件名
//获取第一个“*”的索引
int i = str.indexOf("*");
//获取第一个“.”的索引
int i1 = str.indexOf(".");
//获取第二“.”的索引
int i2 = str.indexOf(".", i1 + 1);
String searchFileName = str.substring(i+1, i2);
//String searchFileName = "";
//String searchFileName = "101800000180";
System.out.println("searchFileName======"+searchFileName);
File folder = new File(folderPath);
if (folder.exists() && folder.isDirectory()) {
searchFiles(folder, searchFileName);
} else {
System.out.println("文件夹不存在或不是一个有效的文件夹路径");
}
}
public static void searchFiles(File folder, String searchFileName) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
searchFiles(file, searchFileName); // 递归搜索子文件夹
} else if (file.getName().contains(searchFileName)) {
System.out.println("找到匹配的文件: " + file.getAbsolutePath());
}
}
}
}
}
java中匹配文件夹下面的文件名跟传入的名称
于 2023-09-04 10:46:43 首次发布