001package xFTest;
002 
003import java.io.BufferedReader;
004import java.io.File;
005import java.io.FileInputStream;
006import java.io.InputStreamReader;
007import java.util.ArrayList;
008import java.util.Collections;
009import java.util.Comparator;
010import java.util.HashMap;
011import java.util.Iterator;
012import java.util.List;
013import java.util.Map;
014 
015import org.junit.Test;
016 
017import junit.framework.TestCase;
018 
019public class TextTest extends TestCase{
020 
021    @Test
022    public void test(){
023        try{
024            String encoding="GBK";
025            File file=new File("E://test.txt");
026            if(file.isFile() && file.exists()){ //判断文件是否存在
027                InputStreamReader read = new InputStreamReader(
028                new FileInputStream(file),encoding);//考虑到编码格式
029                BufferedReader bufferedReader = new BufferedReader(read);
030                String lineTxt = null;
031                HashMap<String ,Integer> has = new HashMap<String ,Integer> ();
032                while((lineTxt = bufferedReader.readLine()) != null){
033                   HashMap<String ,Integer> hasMap = this.StatList(lineTxt);
034                   this.statMap(has, hasMap);
035                }
036                read.close();
037                List<Map.Entry<String, Integer>> list = this.sortMap(has);
038                this.printMap(list);
039            }else{
040                System.out.println("找不到指定的文件");
041            }
042        } catch (Exception e) {
043            System.out.println("读取文件内容出错");
044            e.printStackTrace();
045        }
046    }
047     
048    /**
049     * 对数组解析,将单词和数存入到map中
050     * @param str
051     * @return
052     */
053    public  HashMap<String ,Integer> StatList(String str) {
054           HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表
055           String[] slist = str.split(" ");
056           for (int i = 0; i < slist.length; i++) {
057                 if (!has.containsKey(slist[i])) { // 若尚无此单词
058                         has.put(slist[i], 1);
059                 } else {//如果有,就在将次数加1
060                         has.put(slist[i],has.get(slist[i])+1 );
061                }
062             }
063        return has;
064        }
065     
066    /**
067     * 对map进行重新赋值
068     * @param has
069     * @param hasMap
070     * @return
071     */
072    public HashMap<String,Integer> statMap(HashMap<String ,Integer> has, HashMap<String ,Integer> hasMap){
073         Iterator iterator = hasMap.keySet().iterator();
074         while(iterator.hasNext()){
075                 String word = (String) iterator.next();
076                if (!has.containsKey(word)) { // 若尚无此单词
077                 has.put(word, 1);
078         } else {//如果有,就在将次数加1
079                 has.put(word,has.get(word)+1 );
080        }
081      }
082     return has;
083    }
084     
085    /**
086     * 对map进行排序
087     * @param has
088     * @return
089     */
090    public List<Map.Entry<String, Integer>> sortMap(HashMap<String ,Integer> has){
091        List<Map.Entry<String, Integer>> infoIds =
092                new ArrayList<Map.Entry<String, Integer>>(has.entrySet());
093        Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {  
094            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {     
095                return (o2.getValue() - o1.getValue()); //按value排序
096                //return (o1.getKey()).toString().compareTo(o2.getKey());
097            }
098        });
099        return infoIds;
100    }
101     
102    /**
103     * 打印输出结果
104     * @param list
105     */
106    public void printMap(List<Map.Entry<String, Integer>> list){
107        for(int i = 0 ; i <list.size();i++){
108            System.out.println("单词:"+list.get(i).toString());
109        }
110    }
111     
112     
113}
最近下载更多
wngauss  LV4 2020年3月23日
aisuzhen  LV10 2019年9月17日
liuyufei9797  LV1 2019年4月15日
duduyan  LV11 2018年11月2日
12miki  LV12 2018年2月12日
乞丐哥哥  LV23 2017年12月10日
wxsuperwx  LV17 2017年12月6日
soft5200  LV30 2017年12月4日
wgc_jy  LV21 2017年11月10日
酷睿科技  LV11 2017年10月11日
最近浏览更多
1waxzsq212345  LV2 2024年1月16日
interface  LV22 2023年7月9日
xv123456  LV1 2023年6月1日
微信网友_6040315240812544  LV8 2022年10月31日
idm_Sure  LV3 2022年5月13日
暂无贡献等级
2898369623  LV1 2021年10月12日
huaua7676  LV30 2021年9月2日
mengfanyun  LV9 2021年6月18日
20210119yiyi 2021年1月19日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友