日期:2014-05-17 浏览次数:20855 次
public class ExceptionSpider2 {
/**
* @param args
*/
public static void main(String[] args) {
/*map用来存放发现的异常以及每种异常出现的次数*/
Map<String, Integer> map = readException(Thread.currentThread().getContextClassLoader()
.getResource("text.log").getPath());
System.out.println("检索完毕");
System.out.println("一共发现了" + map.size() + "种异常");
for (String exception : map.keySet()) {
int num = map.get(exception);
System.out.println("异常" + exception + "出现了: " + num + "次");
}
}
public static Map<String, Integer> readException(String fileName) {
Map<String, Integer> map = new HashMap<String, Integer>();
BufferedReader br = null;
/*用作map的key*/
String key = null;
/*定义正则表达式*/
Pattern p = Pattern.compile("(\\w+\\.)+\\w+\\.?Exception");
Matcher m = null;
String line = null;
try {
/*从源文件读取数据*/
br = new BufferedReader(new FileReader(Thread.currentThread().getContextClassLoader()
.getResource("text.log").getPath()));
while ((line = br.readLine()) != null) {
m = p.matcher(line);
/*匹配成功进行循环*/
while (m.find()) {
countException(map, m.gro