package io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
public class text5 {
public static void main(String[] args) throws IOException {
//获取一个文本上每个字符出现的次数,将结果写在times.txt上
File file=new File("aa.txt");
FileReader fr=new FileReader(file);
//FileWriter fw=new FileWriter("")
HashMap<Character, Integer>map=new HashMap<Character, Integer>();
int i;
while((i=fr.read())!=-1){
char c=(char)i;
if(map.get(c)==null){
map.put(c, 1);
}
else{
int x=map.get(c)+1;
map.put(c, x);
}
}
Set<Character> keySet = map.keySet();
for (Character character : keySet) {
System.out.println(character+" "+ map.get(character));
}
}
}
|
|