0
点赞
收藏
分享

微信扫一扫

75.统计文件夹中文件出现次数

爱读书的歌者 2022-04-21 阅读 72
package com.itheima.fieldtest;

import java.io.File;
import java.util.HashMap;

public class Test3 {
    public static void main(String[] args) {
         File file=new File("fileModule");
        HashMap<String,Integer> hm=new HashMap<>();
         getCount(hm,file);
        System.out.println(hm);
    }

    private static void getCount(HashMap<String,Integer> hm,File file) {
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()){
                String fileName = f.getName();
                String[] fileNameArr  = fileName.split("\\.");
                if (fileNameArr.length==2){
                    String fileEndName = fileNameArr[1];
                    if (hm.containsKey(fileEndName)){
                        Integer count = hm.get(fileEndName);
                        count++;
                        hm.put(fileEndName,count);
                    }else {
                        hm.put(fileEndName,1);
                    }
                }
            }else {
                getCount(hm,f);
            }
        }
    }
}
举报

相关推荐

0 条评论