文件类型检测

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class FileListCollect {

    public static void main(String[] args) throws Exception {

        File root = new File("D:\01.Working Area\99.Srouce File\crm-sh-dev");
        HashMap<String, Integer> hm = new HashMap<String, Integer>();

        showAllFiles(root, hm);

        Iterator<Entry<String, Integer>> iter = hm.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<String, Integer> entry = (Entry<String, Integer>) iter.next();
            System.out.println("suffix:   " + String.format("%1$-20s", entry.getKey()) + "    count:  " + entry.getValue());
        }

    }

    final static void showAllFiles(File dir, HashMap<String, Integer> hm) throws Exception {
        File[] fs = dir.listFiles();
        for (int i = 0; i < fs.length; i++) {
            System.out.println(fs[i].getAbsolutePath());
            if (fs[i].isDirectory()) {
                try {
                    showAllFiles(fs[i], hm);
                } catch (Exception e) {

                }
            } else {
                String fname = fs[i].getAbsolutePath();
                String suffix = fname.substring(fname.lastIndexOf(".") + 1);
                // System.out.println(suffix);
                if (hm.containsKey(suffix)) {
                    int cut = hm.get(suffix).intValue();
                    hm.put(suffix, cut + 1);
                } else {
                    hm.put(suffix, 1);
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/actioning/p/4402070.html