package test2;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
//2、统计一个文件中各个字母出现次数,包括字符出现次数
public class test03 {
@Test
public void test1() throws IOException {
File file=new File("D:\\IO流练习文件\\20210820.txt");
if(!file.exists()){
System.out.println("目录不存在!");
}
FileInputStream fir=new FileInputStream("D:\\IO流练习文件\\20210820.txt");
int i=0;
HashMap<Character,Integer> map=new HashMap<Character, Integer>();
while((i=fir.read())!=-1){
if((48<i&&i<57)||(65<i&&i<90)||(97<i&&i<122)){
if(map.get((char)i)!=null){
map.put((char)i,map.get((char)i+1));
}else{
map.put((char)i,1);
}
}
}
Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
for(Map.Entry<Character,Integer> s:entrySet){
System.out.println("字母"+s.getKey()+"数量:"+s.getValue());
}
}
}
07-17
2567
08-09
1669