设为首页 加入收藏

TOP

用Java分析C源代码中头文件使用频率
2014-11-23 23:41:55 来源: 作者: 【 】 浏览:7
Tags:Java分析 源代码 文件 使用 频率

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import java.text.DecimalFormat;


public class WordStats {
public static void main(String[] args) throws IOException {
//System.out.println("Hello World!");
//System.out.println(args.length);
// 解析参数
int argc = args.length;
String inputFilename;
String outputFilename;
if (argc == 2) {
inputFilename = args[0];
outputFilename = args[1];
//System.out.println(args[0]);
//System.out.println(args[1]);
} else {
System.out.println("usage: java WordStats ");
return;
}

// 打开输入和输出文件
BufferedReader in = new BufferedReader(new FileReader(inputFilename));
File fout = new File(outputFilename);
PrintWriter out = new PrintWriter(new FileWriter(fout));

// 使用HashMap来保存统计数据
Map stats = new HashMap();
String str;
Integer count;
// 解析输入文本并统计
while ((str = in.readLine()) != null) {
//System.out.println(str);
if (stats.containsKey(str)) {
count = (Integer)stats.get(str);
count++;
stats.put(str, count);
} else {
count = 1;
stats.put(str, count);
}
}

// 使用ArrayList来对Value进行排序
ArrayList> l = new ArrayList>(stats.entrySet());
Collections.sort(l, new Comparator>() {
public int compare(Map.Entry o1, Map.Entry o2) {
return (o2.getValue() - o1.getValue());
}
});

// 将统计结果保存到输出文件
for (Entry e : l) {
//System.out.println(new DecimalFormat("000").format(e.getValue()) + " " + e.getKey());
out.println(new DecimalFormat("000").format(e.getValue()) + " " + e.getKey());
}

// 关闭输入和输出文件
in.close();
out.close();
}
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇关于Spring中多线程下注入失败的.. 下一篇使用Spring Boot快速启动Spring应..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: