自己做个搜索引擎
使用到的工具 File, FileFilter
话不多说上代码
public class test {
static int count=0;
public static void main(String[] args) {
File file=new File("E:\\");//指定要查询的盘符
showAllFile(file);//调用搜索函数
System.out.println(count);//打印一共有多少个这样的文件
}
public static void showAllFile(File file){//过滤器,对条件进行限制
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory()){
return true;
}
if(pathname.isFile()){//判断是否是文件夹
if(pathname.getName().endsWith(".txt")){//判断是否是以.txt文件结尾的
return true;//返回true时保留这个文件
}
}
return false;
}
});
if(files!=null){
for (File f : files) {
if(f.isFile()){
count++;
System.out.println(f.getAbsolutePath());
}else{
showAllFile(f);//如果还是文件,则继续遍历
}
}
}
}
}