萌新java file练习错误

 

File[] fs = f.listFiles();   这个前面应该要先判断 f.isDirectory()

还有if (file.isFile()) 的else那边  应该是count(file, tm);

public class cs {
    public static void main(String[] args) throws IOException {
        File f = new File("C:\\Lx");
        HashMap<String, Integer> tm = new HashMap<>();
        count(f, tm);
        System.out.println(tm);
    }

    private static void count(File f, HashMap<String, Integer> tm) {
        File[] fs = f.listFiles();
        for (File file : fs) {
            if (file.isFile()) {
                String[] split = file.getName().split("\\.");
                if (tm.containsKey(split[1])) {
                    Integer i = tm.get(split[1]);
                    i++;
                    tm.put(split[1], i);


                } else {
                    tm.put(split[1], 1);
                }

            } else {
                count(f, tm);
            }

        }


    }
}

 

delete