工具類之----排查日誌自己寫的篩選工具類

某項目中因爲日誌比較龐大,肉眼篩選有些困難,所以自己寫了一個工具,如下,留着以後備用

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();
            }
        }
    }

}

這個工具是多次執行,將某一行的數據,一點一點細化。最終輸出。也是讀取文件的一個小練習。留着以後有機會用 

 

發佈了70 篇原創文章 · 獲贊 1037 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章