package zhuofai512;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class testFilter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
File file = new File(str1);
Map<String, Integer> map = new HashMap<String,Integer>();
System.out.println(getAllJava(file,map));
Set<String > set = map.keySet();
for(String s:set) {
System.out.println(s+" "+map.get(s));
}
}
private static long getAllJava(File file,Map<String,Integer> map) {
List<String > list = new ArrayList<String>();
// TODO Auto-generated method stub
long d = 0;
if (file.exists()) {
System.out.println(file);
File f[] = file.listFiles(new MyJavaFilter());
for(File fs:f) {
if (fs.isDirectory()) {
getAllJava(fs,map);
}
else {
d = d + fs.length();
String temp = fs.getName();
temp = fs.getName().substring(fs.getName().lastIndexOf(".")+1, fs.getName().length());
System.out.println(" "+fs);
boolean panduan = false;
for(String s:list) {
panduan = s.equals(temp);
if (panduan) {
break;
}
}
if (!panduan) {
list.add(temp);
map.put(temp, 1);
}
else {
Integer integer = map.get(temp);
integer++;
map.put(temp, integer);
}
}
}
}
return d;
}
}
直接上代码今天写黑马老师的作业,感觉自己又回来了虽然写了好长时间,仔细考虑一定能写出来加油!!!
5.键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key,
最新推荐文章于 2019-03-03 00:14:44 发布