public static void main(String[] args) throws Exception {
BufferedReader br=new BufferedReader(new FileReader("xxx.txt")); //读取文件
String s="";
String line;;
while((line=br.readLine())!=null){
s=(line+s);
}
char[] arr=s.toCharArray(); //将字符串转换成字符数组
HashMap<Character,Integer> map=new HashMap<>(); //将字符设置为键 次数设置为值
for(char st:arr){
if(!map.containsKey(st)){
map.put(st, 1);
}else{
map.put(st, map.get(st)+1); //如果存在 值就加1
}
}
br.close();
for(Character c:map.keySet()){
System.out.println(c+"="+map.get(c));
//};
BufferedWriter bw=new BufferedWriter(new FileWriter("yyy.txt"));
bw.write(c+"="+map.get(c));
bw.flush();//刷新流
bw.close();//关闭流
}
}
}
f=3
d=3
e=3
b=4
c=3
a=8
|
|