您的位置:首页 > 其它

探索软件工程道路上的我 IV (Θ∀Θ#)

2016-03-29 22:39 323 查看
开发语言:Java

开发工具:UltraEdit

小伙伴博客:http://www.cnblogs.com/hyating/

github地址https://github.com/JUNYU217/2016-03-08

|| 为月末了,网费欠了...很抱歉的拖了那么久的作业 QAQ ||

------个人小结------

通过这次作业,我学习了文件流的使用,也感谢小伙伴的帮忙,给了我构思的框架

------作业要求------

基于作业3的结果,读取一个较小的文本文件,统计该文件中的单词的频率,并将统计结果输出到当前目录下的 Result1.txt 文件。 (第一阶段初稿完成该要求)


首先按照频率由高到低排序

频率一样的词, 按照字典顺序排序

详情戳这!

------程序编写------

这次的作用基于第三次作业上有所改动,具体方法在代码中体现

首先感谢我的舍友苗中锋提供给我的排序算法,我一直纠结了好久,通过键排序,通过值排序一直达不到排序的要求

List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
{
if(o1.getValue()<o2.getValue())
return 1;
else if(o1.getValue()>o2.getValue())
return -1;
else
return(o1.getKey().compareTo(o2.getKey()));
}
} );


排序后,通过java中 io 提供的方法完成了输出

File outfile = new File("D:\\Software\\SorfwareTest\\Result.txt");
try
{
if (file.exists()) outfile.delete();
BufferedWriter bw = new BufferedWriter(new FileWriter(outfile));
StringBuffer out = new StringBuffer();
for(Map.Entry<String,Integer> mapping:list)
{
out.append(mapping.getKey()+":"+mapping.getValue()+"\r\n");
}
bw.write(out.toString());
bw.flush();
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
});


------程序运行------

未编译前





在编译和运行后产生了 Result.txt



打开Result.txt,结果是正确的~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: