某项目中因为日志比较庞大,肉眼筛选有些困难,所以自己写了一个工具,如下,留着以后备用
package com.mage.log;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class FileSearch {
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
BufferedOutputStream or = null;
BufferedReader br = null;
try {
//输入文件
File filePath = new File("C:\\Users\\win7-808\\Desktop\\work\\hf\\现场日志\\20190812_tc\\uri.txt");
//筛选之后输出文件流以及位置
fileOutputStream = new FileOutputStream("C:\\Users\\win7-808\\Desktop\\work\\hf\\现场日志\\20190812_tc\\45URI_METHOD.txt");
//输出缓冲流
or = new BufferedOutputStream(fileOutputStream);
//输入缓冲流
br = new BufferedReader(new FileReader(filePath));
//用于接收读入的临时对象String
String s = null;
//第一步 获取conn数值
Set<String> stringConn = new HashSet<String>();//用于接收con
while ((s = br.readLine()) != null) {
//查询只有DEL 的数据行 的conn数值放入stringConn
if(s.indexOf("Method") != -1){
String con = s.substring(s.indexOf("URI"));
stringConn.add(con+ "\r\n");
// System.out.println(con + "\r\n");
}
}
or.write((stringConn + "\r\n").getBytes());
System.out.println(stringConn + "\r\n");
//========================================筛选ip==============================================
//第一步 获取conn数值
/* Set<String> stringConn = new HashSet<String>();//用于接收con
while ((s = br.readLine()) != null) {
//查询只有DEL 的数据行 的conn数值放入stringConn
if(s.indexOf("DEL ") != -1){
String con = s.substring(s.indexOf("conn="),s.indexOf(" op"));
stringConn.add("\""+con+"\"");
// or.write((s + "\r\n").getBytes());
// System.out.println(con + "\r\n");
}
}
System.out.println(stringConn + "\r\n");
*/
//第二部获取含有conn数值的文件
/*while ((s = br.readLine()) != null) {
//过滤含有 conn的指定文件的行
String [] usN = {"conn=177039", "conn=9381", "conn=7481", "conn=10681629", "conn=10753179", "conn=335931", "conn=337662"
};
List<String> resultList= new ArrayList<>(Arrays.asList(usN));
for(String usTem : resultList){
if(s.indexOf(usTem) != -1){
or.write((s + "\r\n").getBytes());
System.out.println(s + "\r\n");
}
}
}*/
//第三步循环筛选出含有指定IP数值的 结果
/*while ((s = br.readLine()) != null) {
//过滤含有指定文件的行
if(s.indexOf("IP") != -1){
or.write((s + "\r\n").getBytes());
System.out.println(s + "\r\n");
}
}*/
//========================================筛选ip end==============================================
or.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
/**
* 关流大法:1,先开后关,先开的输入流,再开的输出流
* 先关外层,再关内层。如BufferedInputStream包装了一个FileInputStream,那么先关BufferedInputStream,再关FileInputStream再关FileInputStream。但要注意的是由于一般处理流持有节点流引用,处理流都会在自己的close方法中去关闭节点流,因此我们只要关闭外层的处理流即可,如果多此一举的关闭节点流反而会报错。如BufferedInputStream包装了FileInputStream,我们只要关闭BufferedInputStream即可
*/
try {
if(or != null){
or.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
if(br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这个工具是多次执行,将某一行的数据,一点一点细化。最终输出。也是读取文件的一个小练习。留着以后有机会用