public class Ex3 {
static Map<String, Integer> map = new HashMap<>();
public static void main(String[] args) {
//要统计的路径
//String path = "D:\\源码\\dormitory";
//count(new File(path));
count(new File("D:\\"));
System.out.println(map);
}
public static void count(File dir) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
count(f);
} else if (f.isFile()) {
String filename = f.getName().toLowerCase();
String ext = "unknow";
try {
ext = filename.substring(filename.lastIndexOf("."));
} catch (Exception e) {
}
if (map.containsKey(ext)) {
Integer t = map.get(ext);
map.put(ext, t + 1);
} else {
map.put(ext, 1);
}
}
}
}
}
编写程序,实现统计某目录的所有文件类型的个数也包括子目录。输入结果如下:Jpg 6Gif 1Png 33Java 5Unknow 3
最新推荐文章于 2023-05-18 13:21:48 发布