public static void main(String[] args) throws IOException {
File dir = getDir();
subFile(dir);
BufferedReader br = new BufferedReader(new FileReader("dir"));
HashMap<Character, Integer> hm = new HashMap<>();
int i;
while ((i = br.read()) != -1) {
char ch = (char) i;
hm.put(ch, !hm.containsKey(ch) ? 1 : hm.get(ch) + 1);
}
br.close();
for (Character c : hm.keySet()) {
BufferedWriter bw = new BufferedWriter(new FileWriter("dir.txt"));
switch (c) {
case '\t':
bw.write("\\t" + "=" + hm.get(c) + 1);
break;
case '\n':
bw.write("\\n" + "=" + hm.get(c) + 1);
break;
case '\r':
bw.write("\\r" + "=" + hm.get(c) + 1);
break;
default:
bw.write(c + "=" + hm.get(c));
break;
}
bw.newLine();
bw.close();
}
}
public static File getDir() {
String s = "D:\\基础班教学";
File dir = new File("s");
if (!dir.exists()) {
System.out.println("你定义的文件路径不存在请重新定义");
} else if (dir.isFile()) {
System.out.println("你定义的一个文件路径请重新定义一个文件夹路径");
} else {
return dir;
}
return dir;
}
public static void subFile(File dir) {
File[] subFiles = dir.listFiles();
for (File file : subFiles) {
if (file.isFile() && file.getName().endsWith(".java")) {
System.out.println(file);
} else if (file.isDirectory()) {
subFile(file);
}
}
}
}
|
|