import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExceptionSpider2 {
public static void main(String[] args) {
/*map用来存放发现的异常以及每种异常出现的次数*/
Map<String, Integer> map = new HashMap<String, Integer>();
/**/
BufferedReader br = null;
/*存放从文件中读取的数据行*/
String line = null;
/*用作map的key*/
String key = null;
/*定义正则表达式*/
Pattern p = Pattern.compile("(\\w+\\.)+\\w+\\.?Exception");
Matcher m = null;
try {
/*从源文件读取数据*/
br = new BufferedReader(new FileReader("E:\\text.log."));
while((line=br.readLine()) !=null) {
/**/
m = p.matcher(line);
/*匹配成功进行循环*/
while(m.find()) {
key = m.group();
/*判断是否包含key*/
if(map.containsKey(key)) {
/*包含key则value加1*/
map.put(key, map.get(key) + 1);
}else {
/*不包含则创建key*/
map.put(key, 1);
}
}
}
/*关闭打开的文件*/
br.close();
System.out.println("检索完毕");
System.out.println("一共发现了" + map.size() + "种异常");
/*遍历map*/
Iterator it = map.keySet().iterator();
while(it.hasNext()) {
String exception = (String) it.next();
int num = map.get(exception);
System.out.println("异常" + exception + "出现了: " + num + "次");
}
//System.out.println(map);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}从log中查询exception出现的种类以及次数。请帮我完善下或改下格式。看着挺乱。
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExceptionSpider2 {
public static void main(String[] args) {
/*map用来存放发现的异常以及每种异常出现的次数*/
Map<String, Integer> map = new HashMap<String, Integer>();
/**/
BufferedReader br = null;
/*存放从文件中读取的数据行*/
String line = null;
/*用作map的key*/
String key = null;
/*定义正则表达式*/
Pattern p = Pattern.compile("(\\w+\\.)+\\w+\\.?Exception");
Matcher m = null;
try {
/*从源文件读取数据*/
br = new BufferedReader(new FileReader("E:\\text.log."));
while((line=br.readLine()) !=null) {
/**/
m = p.matcher(line);
/*匹配成功进行循环*/
while(m.find()) {
key = m.group();
/*判断是否包含key*/
if(map.containsKey(key)) {
/*包含key则value加1*/
map.put(key, map.get(key) + 1);
}else {
/*不包含则创建key*/
map.put(key, 1);
}
}
}
/*关闭打开的文件*/
br.close();
System.out.println("检索完毕");
System.out.println("一共发现了" + map.size() + "种异常");
/*遍历map*/
Iterator it = map.keySet().iterator();
while(it.hasNext()) {
String exception = (String) it.next();
int num = map.get(exception);
System.out.println("异常" + exception + "出现了: " + num + "次");
}
//System.out.println(map);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}从log中查询exception出现的种类以及次数。请帮我完善下或改下格式。看着挺乱。
解决方案 »
- 急,奇怪的Parameter #1 has not been set.的问题?
- ActiveMQ,consumer端配合多线程读取数据该如何做?
- 关于JAVA类名的定义
- j2se学完然后学什么,再然后学什么
- 实在不行了... ...
- 完成一个数据报告后,怎么样提供HTML格式预览和EXCEL格式下载?
- 请问这两段代码的区别
- POI 中如何设置列宽?急问!
- JSP聊天室中如何制作不屏闪(刷新)的 对话显示区-------千万火急
- 如何实现类似电子邮局中传输附件,也就是文件上传到服务器的功能?
- 高手请进, struts2 checkbox如何动态赋值
- jquery easyui-datebox没有效果
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.group());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
/*关闭打开的文件*/
try {
br.close();
} catch (IOException e) { }
}
return map;
}
private static void countException(Map<String, Integer> map, String key) {
System.out.println(key);
/*判断是否包含key*/
if (map.containsKey(key)) {
/*包含key则value加1*/
map.put(key, map.get(key) + 1);
} else {
/*不包含则创建key*/
map.put(key, 1);
}
}
}
输入数据java.io.FileNotFoundException java.io.IOException
java.io.RuntimeException java.io.RuntimeException
java.io.RuntimeException输出结果:java.io.FileNotFoundException
java.io.IOException
java.io.RuntimeException
java.io.RuntimeException
java.io.RuntimeException
检索完毕
一共发现了3种异常
异常java.io.RuntimeException出现了: 3次
异常java.io.FileNotFoundException出现了: 1次
异常java.io.IOException出现了: 1次
这个异常是由于参数错误导致的。怎么更细化的把这个异常里每种参数错误的出现次数统计一下呢。是不是要用到正则表达式对异常更加严格的匹配?
比如说javax.xml.rpc.ServiceException: /rest/online 和
javax.xml.rpc.ServiceException: /rest/ads 在查找的时候并没有把异常的总名称查询出来。如何更细致的查询呢