/**
* @param args
* 获取一个文本上每个字符出现的次数,将结果写在times.txt上
*
* 1,创建带缓冲区的输入流对象
* 2,创建双列集合对象,目的是把字符当作键,把字符出现的次数当作值
* 3,通过读取不断向集合中存储,存储的时候要判断,如果不包含这个键就将键和值为1存储,如果包含就将键和值加1存储
* 4,关闭输入流
* 5,创建输出流对象
* 6,将结果写出
* 7,关闭输出流
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
HashMap<Character, Integer> hm = new HashMap<>();
int len;
while ((len = br.read()) != -1){
char c = (char)len;
if(!hm.containsKey(c)){
hm.put(c, 1);
}else{
hm.put(c, hm.get(c)+1);
}
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
for (Character key : hm.keySet()) {
bw.write(key+"--"+hm.get(key));
bw.write("\r\n");
}
bw.close();
}
|
|