* 13、获取一个文本上每个字符出现的次数,将结果写在times.txt上
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
String line = br.readLine();
char[] c = line.toCharArray();
// System.out.println(c);
// Character c1 = Character.
HashMap<Character, Integer> hm = new HashMap<>();
for(char c1 : c){
if(!hm.containsKey(c)){
hm.put(c1, 1);
}else{
hm.put(c1, (hm.get(c)+1));
}
}
String s = hm.toString();
BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
bw.write(s);
bw.newLine();
br.close();
bw.close();
}
xxx.txt ; aaabbc
输出结果:
times.txt : {b=1, c=1, a=1}
请问我这个做法有什么问题吗? |
|