在日常工作中经常会统计一些数据,下面介绍一个例子,如何统计某个目录下边各种文件类型的文件数量。
本例子使用到了递归的算法,字符串的常用运算,文件的处理以及集合类的使用。是一个比较好的学习例子。
/** * 统计某个目录下所有各种文件类型的数量 */ public class StaticFile { public static void main(String[] args) { /** * */ HashMap<String ,FileCnt> myFilemap = new HashMap<String ,FileCnt>(); staticFile(myFilemap,"D:\\untitled"); myFilemap.forEach((x,y)->{ System.out.println("filetype : " +x + " num :" + y ); }); } /** * * @param fileMap * @param filePath */ static void staticFile(HashMap<String ,FileCnt> fileMap,String filePath ){ File file = new File(filePath); if (file.exists()){ // 如果是目录 则递归调用 if(file.isDirectory()){ String[] list = file.list(); for (String afile:list) { staticFile(fileMap,filePath + "\\" + afile ); } // 如果是文件 则处理map集合中的数据 }else { // 获取扩展名 以及文件名称 int lastgg = filePath.lastIndexOf("\\"); if (lastgg <=0 ) return; // 得到文件的名称 String fileName = filePath.substring(lastgg+1); // 没有扩展名的情况 String suffix = "notype"; //获取扩展名 if(fileName.lastIndexOf('.')>0){ try { suffix = fileName.substring(fileName.lastIndexOf('.' )+1); }catch (StringIndexOutOfBoundsException e){ System.out.println("filename error : " + fileName); } } // 如果该类型已经存在于map中 // 则原有的基础之上自增 // 否则创建新的对象保存在map中 if (fileMap.get(suffix)==null){ fileMap.put(suffix,new FileCnt(suffix,1)); }else{ fileMap.get(suffix).increaseNum(); } } } } } /** * 保存文件类型与数目的结构 */ class FileCnt{ String suffix;// 扩展名 int num;// 文件的个数 @Override public String toString() { return "FileCnt{" + "suffix='" + suffix + '\'' + ", num=" + num + '}'; } public FileCnt(String suffix, int num) { this.suffix = suffix; this.num = num; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public void increaseNum() { this.num++; } }