=读取日志文件,正则表达式匹配出有用信息,并且保存到csv文件。

//版本1


import java.io.BufferedReader;  
import java.io.BufferedWriter;
import java.io.File;  
import java.io.FileReader;  
import java.io.FileWriter;
import java.io.IOException;  
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class FileRead {  
//这是一个读取文件的类
static String LogfileName = "";  
static Map<String,String>peizhis=new HashMap<String,String>();
public static void readFile(nowFile nowfile, String Opreate) throws IOException {  
File f = new File(nowfile.getPath());
FileReader file = new FileReader(f);
BufferedReader br = new BufferedReader(file);
ArrayList <String>duqu=new ArrayList<String>();
while (br.ready() ){
duqu.add(br.readLine());
}  
if(Opreate.equals("File")){
nowfile.setFileList(duqu);
Cut cut=new Cut(nowfile);
Save save=new Save();
nowfile.setFileList(null);
nowfile.setDesription(cut.desription);
save.SaveKeywordsOrToCity(nowfile,"desription");
}else if(Opreate.equals("peizhi")){
//取出配置文件的内容保存到Map
for(int i=0;i<duqu.size();i++){
String Array[]=duqu.get(i).split("#");
peizhis.put(Array[0],Array[1]);
}
}
br.close();  
file.close();  
}  


public static void main(String[] canshu) throws IOException { 
// String canshu[]={"start-2015-02-05","start-2015-08-05"};

//读取配置文件
String peizhi = "D:\\peizhi.ini"; 
try{
nowFile nowfile=new nowFile();
nowfile.setPath(peizhi);
readFile(nowfile,"peizhi");
}catch(Exception e){
e.printStackTrace();
System.out.println("配置文件不存在,请到D:/peizhi.ini 配置好文件");
Save save=new Save();
save.CreatePeizhi(peizhi);
System.exit(0);
}
if(!(canshu.length==2)){
System.out.println("没有写入扫描时间段,格式:start-2000-01-01 stop-2011-11-11");
System.exit(0);
}else{
System.out.println("正在扫描...");
}
String path=peizhis.get("Path");
//按照文件名读取有久读取,没有就不读取
int FileCount=0;
ArrayList<String> dates=DateTime(canshu);
for (int i = 0; i < dates.size(); i++) {
try{
File file=new File(path+"-"+dates.get(i));
nowFile nowfile=new nowFile();
nowfile.setName(file.getName());
nowfile.setPath(file.getPath());
nowfile.setStartDatedate(canshu[0]);
nowfile.setStopDatedate(canshu[1]);
nowfile.setSavePath(peizhis.get("SavePath"));
readFile(nowfile,"File");
FileCount++;
}catch(Exception e){

}
}
System.out.println("扫描结束总共扫描到"+FileCount+"个文件");
/*
* 读取指定文件夹下的内容并且匹配

String path=peizhis.get("Path");


File file=new File(path);
File[] tempList = file.listFiles();
System.out.println("该目录下对象个数:"+tempList.length);
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("文     件:"+tempList[i]);
if(compareTime(canshu[0],canshu[1],tempList[i])){
nowFile nowfile=new nowFile();
nowfile.setName(tempList[i].getName());
nowfile.setPath(tempList[i].getPath());
nowfile.setStartDatedate(canshu[0]);
nowfile.setStopDatedate(canshu[1]);
readFile(nowfile,"File");
}       
}
}
*/
}  


//遍历两个时间内日期的函数
public static ArrayList<String>  DateTime(String date[]) {  
ArrayList<String> dates=new ArrayList<String>();
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
Long oneDay = 1000 * 60 * 60 * 24l;  
//得到开始时间

String []ymd =date[0].split("-");
start.set(Integer.parseInt(ymd[1]),Integer.parseInt(ymd[2])-1, Integer.parseInt(ymd[3]));  
Long startTIme = start.getTimeInMillis();  
//得到结束时间

String  []ymdend =date[1].split("-");
end.set(Integer.parseInt(ymdend[1]),Integer.parseInt(ymdend[2])-1, Integer.parseInt(ymdend[3]));  
Long endTime = end.getTimeInMillis();  





Long time = startTIme;  
while (time <= endTime) {  
Date d = new Date(time);  
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
dates.add(df.format(d));
time += oneDay;  
}

return dates;

}  

文件是这样的


//比较文件时间与配置文件时间返回真假的函数
public static boolean compareTime(String startDate,String stopDate,File file){
boolean result=false;
/*
* 按照文件生成日期比较大小

//获得文件时间并且把文件时间转换成string
long time = file.lastModified();   
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);     
String fileTime=(formatter.format(cal.getTime())); 
//获取配置文件的开始时间和结束时间
// String startDate=peizhis.get("StartDate");
// String stopDate=peizhis.get("StopDate");


//把文件时间把配置文件时间都转换为long
long fileTi =Long.parseLong(fileTime.replace(":","").replace(" ","").replace("-", ""));
long start =Long.parseLong(startDate.replace(":","").replace(" ","").replace("-", "")); 
long stop =Long.parseLong(stopDate.replace(":","").replace(" ","").replace("-", "")); 
if(fileTi>start&&fileTi<stop){
result=true;
}
*/


//按照文件名称时间读取


return result;


}


}  

文件样式


class Cut {
//这是一个处理日志的类正则出可用信息
Map <String,Integer> toCityMap=new HashMap<String,Integer>();
Map <String,Integer>KeyWordsMap=new HashMap<String,Integer>();
String desription="";
static int  KeyWords=0;
static int  toCity=0;
static int search=0;
public Cut(nowFile file) {
System.out.println("扫描到"+file.getPath()+"\n开始读取...");
Iterator<String> it=file.getFileList().iterator();
//读取文件里的行数
while(it.hasNext()){
FindgetSearch(it.next());
}
desription="本次扫描:"+file.getFileList().size()+"条\n search:   "+search+"条\n toCity    "+toCity+"条 \n KeyWords    "+search;
file.setFileList(null);
if(toCityMap!=null){


//对toCity进行排序且保存
paixu px=new paixu();
Save save=new Save();
Map<String,Integer> paixutoCity=px.mapPaixu(toCityMap);
file.setResult(paixutoCity);
save.SaveKeywordsOrToCity(file,"toCity");
//初始化
toCity=0;
search=0;
paixutoCity.clear();
toCityMap.clear();
}else{
System.out.println("Tocity不存在");
}
if(KeyWordsMap!=null){
paixu px=new paixu();
Save save=new Save();
//对keywords进行排序且保存
Map<String,Integer> paixuKeyWordsMap  =  px.mapPaixu(KeyWordsMap);
file.setResult(paixuKeyWordsMap);
save.SaveKeywordsOrToCity(file,"KeyWords");
//初始化
KeyWords=0;
search=0;
paixuKeyWordsMap.clear();
KeyWordsMap.clear();
}else{
System.out.println("KeyWords不存在");
}
System.out.println("扫描结束,新文件保存在"+file.getSavePath());
}
//查找 存在GET /search/list的行数
public void FindgetSearch(String oneline) {
String str=oneline;
Pattern p = Pattern.compile("GET /search/list([^<]*) HTTP"); 
Matcher m = p.matcher(str); 
while(m.find()) {
//取出get /search后分开处理ToCity和KeyWords
toCity(m.group(1));
KeyWords(m.group(1));
search++;
}     
}
//这是一个匹配toCity的函数
public void toCity(String oneline) {
String str=oneline;
Pattern p = Pattern.compile("to_city=%([^<]*)"); 
Matcher m = p.matcher(str); 
while(m.find()) { 
String str2=oneline;
Pattern pattern = Pattern.compile("to_city=([^<]*)&t"); 
Matcher matcher = pattern.matcher(str2); 


while(matcher.find()) { 
toCity++;
String toCity=matcher.group(1).split("&")[0];
try {
toCity   =   java.net.URLDecoder.decode(toCity,   "utf-8");
} catch (UnsupportedEncodingException e) {


e.printStackTrace();
}
if(toCityMap.containsKey(toCity)){
toCityMap.put(toCity,toCityMap.get(toCity)+1);
}else{
toCityMap.put(toCity, 1);
}
}


}


}
//这是一个匹配KeyWords的函数
public void KeyWords(String oneline) {
Pattern p = Pattern.compile("keyword=%([^<]*)"); 
Matcher m = p.matcher(oneline); 
while(m.find()) {
KeyWords++;
String KeyWord="%"+m.group(1).split("&")[0];
try {
KeyWord   =   java.net.URLDecoder.decode(KeyWord,   "utf-8");
} catch (Exception e) {
}
if(KeyWordsMap.containsKey(KeyWord)){
KeyWordsMap.put(KeyWord,KeyWordsMap.get(KeyWord)+1);


}else{
KeyWordsMap.put(KeyWord, 1);
}
}


}
}


class paixu {
public Map <String,Integer>mapPaixu(Map <String,Integer>oldMap){
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet());  
       Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {  
 
           @Override  
           public int compare(Entry<java.lang.String, Integer> arg0,Entry<java.lang.String, Integer> arg1) {  
               return arg1.getValue() - arg0.getValue();  
           }  
       });  
       Map <String,Integer>newMap = new LinkedHashMap<String,Integer>();  
       for (int i = 0; i < list.size(); i++) {  
           newMap.put(list.get(i).getKey(), list.get(i).getValue());  
       }  
       return newMap;  
   }  
}


class Save {


//保存 Keywords和ToCity


public  void SaveKeywordsOrToCity(nowFile file,String leixing)  {
try {
//保存文件路径
String toCityPath=FileRead.peizhis.get("SavePath")+Data()+"/"+"YouddToCity";
String KeyWordsPath=FileRead.peizhis.get("SavePath")+Data()+"/"+"YouddKeyWords";


//检查及创建文件夹
isExist(toCityPath);
isExist(KeyWordsPath);
String fileName ="";
if(leixing.equals("toCity")){
fileName = toCityPath+"/"+file.getName()+leixing+".csv";
}else if(leixing.equals("KeyWords")){
fileName =KeyWordsPath+"/"+file.getName()+leixing+".csv";
}else if(leixing.equals("desription")){
fileName =FileRead.peizhis.get("SavePath")+Data()+"/"+"logs.txt";
File outputFile = new File(fileName);
outputFile.createNewFile();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)), true);        
out.println(file.getDesription()+"开始时间"+file.getStartDatedate()+"结束时间"+file.getStopDatedate());
out.close();
return;
}




// 进行file的初始化...
File outputFile = new File(fileName);
if(!outputFile.exists()){
outputFile.createNewFile();
}else{
System.out.println("fileName"+leixing+"已经存在");
}
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)), true);  
out.println(" "+","+"城市"+","+"搜索次数");
Set<String> toCitykeys = file.getResult().keySet();       
for(String key :toCitykeys){
out.println(leixing+","+key+","+file.getResult().get(key));
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void CreatePeizhi(String peizhi){
try {
File outputFile = new File(peizhi);
PrintWriter out;
outputFile.createNewFile();
out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)), true);
out.println("Path#");
out.println("SavePath#");
} catch (IOException e) {
e.printStackTrace();
}  
}
// 获得 当前时间的方法
private String Data() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return dateString;
}
public static  void isExist(String path) {


File file = new File(path);
//判断文件夹是否存在,如果不存在则创建文件夹
if  (!file .exists()  && !file .isDirectory())      
{
file.mkdirs();
file.mkdir();
} else   
{  
}  
}
}




class nowFile {
private String path;
private String SavePath;
private String name;
private Date DuquDate;
private String startDatedate;
private String stopDatedate;
private  String desription;
private ArrayList <String>fileList;
private Map<String,Integer> result=new HashMap<String,Integer>();
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSavePath() {
return SavePath;
}
public void setSavePath(String savePath) {
SavePath = savePath;
}


public Date getDuquDate() {
return DuquDate;
}
public void setDuquDate(Date duquDate) {
DuquDate = duquDate;
}

public ArrayList<String> getFileList() {
return fileList;
}
public void setFileList(ArrayList<String> fileList) {
this.fileList = fileList;
}
public Map <String,Integer>getResult() {
return result;
}
public void setResult(Map<String,Integer> result) {
this.result = result;
}
public String getStartDatedate() {
return startDatedate;
}
public void setStartDatedate(String startDatedate) {
this.startDatedate = startDatedate;
}
public String getStopDatedate() {
return stopDatedate;
}
public void setStopDatedate(String stopDatedate) {
this.stopDatedate = stopDatedate;
}
public String getDesription() {
return desription;
}
public void setDesription(String desription) {
this.desription = desription;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值