处理日志小工具:GetFromFile快速从日志大文件中找到想要的报错信息

处理日志小工具:GetFromFile文件IO工具类-可以处理数据量较大的文件


package com.bankcomm.pccc.esb;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 
 * @author yangcq
 * 
 * 处理大数据量文件工具类
 * 
 * 应用场景:
 * 1,从50M以上的日志文件中,查找想要的内容,如果这个内容的提取逻辑很复杂,这时候一般的工具就难以胜任了,我们就可以考虑使用这个小程序来处理;
 * 2,处理大批量的报表文件或者订单文件,比如从商城的订单汇总txt文件中取出所有的金额字段;
 * 3,其他难以处理的大批量文件;
 *
 */
/*public class GetFromFile {
    
    //文件路径,从这个文件读取内容,也就是我们需要处理的文件
    private static String FILE_PATH = "D:\\livePayment-addAccount.txt";
    //文件输出流,输出到文件,输出处理以后的内容到这个文件
    private static String FINAL_FILE_PATH = "D:\\text.txt";
    //起始位置
    private static int START_POSITION = 6;
    //结束位置
    private static int END_POSITION = 14;
    
    public static void main(String[] args)throws Exception{
        FileReader fr=new FileReader(FILE_PATH);
        BufferedReader bufferedReader = new BufferedReader(fr);
        FileOutputStream fileOutputStream = new FileOutputStream(new File(FINAL_FILE_PATH));
        PrintStream printStream = new PrintStream(fileOutputStream);
        //一行内容,原始内容
        String line = "";
        //截取以后的内容,也就是我们想要的内容部分
        String finalString = "";

        *//**
         * while((line = bufferedReader.readLine()) != null) 与 while(bufferedReader.readLine() != null)
         * 的区别
         *//*
        while((line = bufferedReader.readLine()) != null){
        //while(bufferedReader.readLine() != null){  
            //1,读取文件,以行为单位
            //line = bufferedReader.readLine();
            
            //截取字符串,取出我们想要的内容
            *//**
             * Returns the length of this string.
             * The length is equal to the number of Unicode code units</a> in the string.
             *  public int length() {
             *     return count;
             *  }
             *//*
            if(line.length() > END_POSITION){
                finalString = line.substring(START_POSITION, END_POSITION);
            }
            else if(line.length() > START_POSITION && line.length() < END_POSITION){
                finalString = line.substring(START_POSITION, line.length());
            }
            else{
                finalString = "";
            }
            if(!"".equals(line) && line != null){
                finalString = line.substring(START_POSITION, END_POSITION);
            }
            
            //输出到文件
            printStream.println(finalString);
        }
        //关闭文件输入流
        bufferedReader.close();
        //关闭文件输出流
        printStream.close();
    }
}*/

/**
 * 
 * 向文件每一行批量加入一个字符串
 * 
 * 读取文件,以行为单位,向每一行行尾加入固定字符串  STRING_APPENDED_TO_END_OF_LINE = "  已修改";
 */
/*public class GetFromFile {
    
    //文件路径,从这个文件读取内容,也就是我们需要处理的文件
    private static String FILE_PATH = "D:\\xml.txt";
    //文件输出流,输出到文件,输出处理以后的内容到这个文件
    private static String FINAL_FILE_PATH = "D:\\text.txt";
    //添加的字符串
    private static String STRING_APPENDED_TO_END_OF_LINE = "  已修改";
    
    public static void main(String[] args)throws Exception{
        FileReader fr=new FileReader(FILE_PATH);
        BufferedReader bufferedReader = new BufferedReader(fr);
        FileOutputStream fileOutputStream = new FileOutputStream(new File(FINAL_FILE_PATH));
        PrintStream printStream = new PrintStream(fileOutputStream);
        //一行内容,原始内容
        String line = "";

        while((line = bufferedReader.readLine()) != null){
            *//**
             * 过滤空行(line == null)和空格行(line != null,但是有空格的行)
             *//*
            if(!"".equals(line) && line != null && !"".equals(line.trim())){
                printStream.println(line + STRING_APPENDED_TO_END_OF_LINE);
            }
            else{
                printStream.println(line);
            }
            
            //输出到文件
            printStream.println(line + STRING_APPENDED_TO_END_OF_LINE);
        }
        //关闭文件输入流
        bufferedReader.close();
        //关闭文件输出流
        printStream.close();
    }
}*/

/**
 * 
 * 从日志文件中找出包含指定字符串的报错信息
 * 并且满足以下条件:
 * 1,从ProductUploadController.uploadImg行开始显示;
 * 2,显示报错的完整信息;
 * 
 * 例子:String = ProductUploadController.uploadImg
 * 
 */
public class GetFromFile {
    
    //文件前缀  路径
    private static String FILE_PREFIX = "D:\\";
    
    //文件名
    private static String FILE_NAME = "vendor_server34_20160516";
    
    //文件名后缀
    private static String FILE_SUFFIX = ".log";
    //private static String FILE_SUFFIX = ".txt";
    
    //生成当前日期
    static Date currentSeconds = new Date();
    static SimpleDateFormat  SimpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    static String newCreateDate = SimpleDateFormat.format(currentSeconds);
    
    //文件路径,从这个文件读取内容,也就是我们需要处理的文件
    //private static String FILE_PATH = "D:\\vendor_server34_20160516.log";
    private static String FILE_PATH = FILE_PREFIX + FILE_NAME + FILE_SUFFIX;
    
    //文件输出流,输出到文件,输出处理以后的内容到这个文件
    private static String FINAL_FILE_PATH = FILE_PREFIX + FILE_NAME + "_" + newCreateDate + FILE_SUFFIX;
    
    //指定的字符串
    private static String STRING_TO_FIND = "ProductUploadController.uploadImg";
    
    //总数
    private static int TOTAL = 1;
    
    public static void main(String[] args)throws Exception{
        FileReader fr=new FileReader(FILE_PATH);
        BufferedReader bufferedReader = new BufferedReader(fr);
        FileOutputStream fileOutputStream = new FileOutputStream(new File(FINAL_FILE_PATH));
        PrintStream printStream = new PrintStream(fileOutputStream);
        //一行内容,原始内容
        String line = "";

        //设置一个开关,控制是否输出到文件
        boolean flag = false;
        
        while((line = bufferedReader.readLine()) != null){
            /**
             * 取出所有的报错信息
             */
            if(flag){
                printStream.println(line);
            }
            if(line.trim().contains(STRING_TO_FIND)){
                printStream.println("第" + TOTAL + "处:");
                printStream.println(line);
                TOTAL++;
                flag = true;
            }
            if(line.trim().startsWith("[")){
                flag = false;
            }
        }
        //测试
        System.out.println("一共有:" + TOTAL + " 处!");
        //关闭文件输入流
        bufferedReader.close();
        //关闭文件输出流
        printStream.close();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值