Java之查询指定目录下的文件
package cn.javanet.test; import java.io.*; /**
- 查询指定目录下的所有文件
- @author 杨剑琦 * */ public class FileTest {
/**
- @param args */ public static void main(String args) { // TODO Auto-generated method stub FileTest star=new FileTest(); String path="d:"; File war=new File(path); int count=star.findTxtFileCount(war,"exe"); System.out.println("----: "+count); }
/**
- 创建查询指定目录下文件的方法
- @param allList 指定目录
- @param endName 指定以“”结尾的文件
- @return 得到的文件数目 / public int findTxtFileCount(File allList,String endName){ // int textCount=0; //创建fileArray名字的数组 File fileArray= allList.listFiles(); // 如果传进来一个以文件作为对象的allList 返回0 if(null==fileArray){ return 0; } //偏历目录下的文件 for(int i=0;i<fileArray.length;i++){ //如果是个目录 if(fileArray.isDirectory()){ // System.out.println("目录: "+fileArray*.getAbsolutePath()); //递归调用 textCount+=findTxtFileCount(fileArray*.getAbsoluteFile(),endName); //如果是文件 }else if(fileArray*.isFile()){ //如果是以“”结尾的文件 if(fileArray*.getName().endsWith(endName)){ //展示文件 System.out.println("exe文件: "+fileArray*.getAbsolutePath()); //所有以“”结尾的文件相加 textCount++; } } } return textCount;
} }