标签:
/**
* Project Name:dataanalysis02
* File Name:LogQuery.java
* Package Name:actions
* Date:2016-1-14上午9:26:25
* Copyright (c) 2016, China Link Communications LTD All Rights Reserved.
*
*/
package actions;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import demon.XmlName;
/**
* ClassName: LogQuery <br/>
* Date: 2016-1-14 上午9:26:25 <br/>
* Description: TODO
*
* @author lieg
* @version
* @see
*/
public class LogQuery {
public static void main(String[] args) {
LogQuery logQuery = new LogQuery();
String queryname = "REQUEST-XML";
String searchPath = "/handles.log";
List<Map<String,XmlName>> xmlList = logQuery.findLineByTimeLogAndaName(searchPath);
System.out.println(xmlList.toString());
}
public List<Map<String,XmlName>> findLineByTimeLogAndaName(String serachPath) {
XmlName xmlnamesum = new XmlName();
BufferedReader br = null;
Map<String,XmlName> mapqurey = new HashMap<String,XmlName>();
String queryname = "REQUEST-XML";
String queryname1 = "RESPONSE-XML";
String line = "";
int allnum = 0;
int allnum1 = 0;
String regexdate = ""; //时间的正则表达式
String date ;
int count1;
List<Map<String,XmlName>> querynamesum = new ArrayList();
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(serachPath)));
line = br.readLine();
regexdate = "(\\d{4}-\\d{2}-\\d{2} \\d{2})"; //时间的正则表达式
while (null != line) {
count1 = count(line, queryname);
if(count1 != 0)
{
Pattern pattern = Pattern.compile(regexdate);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String req_service = matcher.group(1);
}
}
allnum += count(line, queryname);
allnum1 += count(line, queryname1);
line = br.readLine();
}
xmlnamesum.setNum(allnum);
xmlnamesum.setQueryName(queryname);
mapqurey.put("00",xmlnamesum);
querynamesum.add(mapqurey);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
return querynamesum;
}
/**
* 在line中查找queryname,并返回找到的个数 line 从文件读入的一行 queryname
* 要查找的字符串 line中queryname的个数
*/
// 每一行的特定字符串的记录数
public int count(String line, String queryname) {
int count = 0;
int len = queryname.length();
int fromIndex = 0;
while ((fromIndex = line.indexOf(queryname, fromIndex) + 1) > 0) {
count++;
}
return count;
}
}
标签:
原文地址:http://www.cnblogs.com/zhuzhuxuan/p/5174911.html