java怎么根据文件格式搜索文件?

来源:百度知道 编辑:UC知道 时间:2024/06/02 14:50:21
也就是说想搜txt文件格式的文件,就可以直接输入*.txt,就可以搜索到,但是用java怎么实现呢?具体的代码。还有当输入aa*.txt时,就关于aa开头的,所有的txt文件都搜索到,最好有具体的代码实现。在此先谢谢啦!
我知道用正则表达式可以解决的,但是我看不懂那,现在也急用啊,就很感谢大家啦!

27.指定目录下搜索文件
//import java.io.*;
private final String filter="*.*";
private int countFiles=0;
private void doSearch(String path){
File file = new File(path);
if(file.exists()) {
if(file.isDirectory()) {
File[] fileArray = file.listFiles();
for(File f:fileArray) {
if(f.isDirectory()) {
doSearch(f.getPath());
} else {
if(f.getName().indexOf(filter) >= 0) {
countFiles++;
result.append(f.getPath() + "\r\n");
}
}
//f.getPath()
}
//"The numbers of files had been found:" + countFiles
} else {
//"Couldn't open the path!"
}
} else {
System.out.println("The path had been apointed was not Exist!");
}
}
doSearch(%%1);

把输入的字符串中.替换成\.然后直接作为正则表达式使用