Java递归模糊查询文件
字符串模糊查询
public boolean matchStringByIndexOf(String str,String part) {
int count = 0;
int index = 0;
while( ( index = str.indexOf(part, index) ) != -1 )
{
index = index+part.length();
count++;
}
if(count < 1){
return false;
}
return true;
}
递归遍历文件
public List searchFileList(File path, String fileName){
File[] files=path.listFiles(); //列出所有的子文件
for(File file :files)
{
if(file.isFile()){//如果是文件,则先模糊查询,判断是否相关
if(stringUtilService.matchStringByIndexOf(file.toString(),fileName)){
fileListData.add(file.toString());
}
}else if(file.isDirectory())//如果是文件夹,则输出文件夹的名字,并递归遍历该文件夹
{
searchFileList(file,fileName);//递归遍历
}
}
return fileListData;
}
测试
public static void main(String[] args) {
String A ="G:/HTML/testData/达芬奇";
File file = new File(A);
FileService fileService = new FileServiceImpl();
System.out.println(fileService.searchFileList(file,"myse").toString());
}