处理特殊log语句的java函数

工作中经常需要将log中的某些有用的信息提取出来以供产品或者决策部门使用,自己写了一段小程序来定制这种信息提取动作。



	    public static boolean dealWithLog()
	    {
	        String charsetName="UTF-8";
	        String FilePath = "E:\\log\\";
	        String FileName = "Idraw.txt";
	        String FileOutName = "Idresult.txt"; 
	        String separatePattern = "发现来自";
	        String pattern_1 = "的恶意用户";
	        String pattern_2 = "在进行短信";
	        String fullFileName = FilePath+FileName;
	        String fullFileOutName = FilePath+FileOutName;
	         
	        BufferedReader d;
	        BufferedWriter w;
	        InputStreamReader insReader;
	        OutputStreamWriter outWriter;
	        String temp;
	        String tempOut;
	        String tempTem;
	         
	        int patternLen = separatePattern.length();
	        int pattern_1Len = pattern_1.length();
	        int pattern_2Len = pattern_2.length();
	         
	        Map<String, String> idMap = new HashMap<String, String>();
	        int patternPos = -1;
	        int pattern_1Pos = -1;
	        int pattern_2Pos = -1;
	         
	        int lineIndex = 0;
	         
	        try
	        {
	            insReader = new InputStreamReader(new FileInputStream(fullFileName), charsetName);
	            outWriter = new OutputStreamWriter(new FileOutputStream(fullFileOutName), charsetName);
	            d = new BufferedReader(insReader);
	            w = new BufferedWriter(outWriter);
	             
	            while(((temp = d.readLine()) != null) && (!temp.equalsIgnoreCase("")))
	            {
	                patternPos = temp.indexOf(separatePattern);
	                pattern_1Pos = temp.indexOf(pattern_1);
	                pattern_2Pos = temp.indexOf(pattern_2);
	                 
	                if (patternPos != -1 &&
	                        pattern_1Pos != -1 &&
	                        pattern_2Pos != -1)
	                {
	                    tempTem = temp.substring(pattern_1Pos+pattern_1Len, pattern_2Pos);
	                    System.out.println("--> No "+lineIndex+" line processed <--");
	                    System.out.println("tempTem: >"+tempTem+"<");
	                    String tmpMapVal = idMap.get(tempTem);
	                    if (StringUtils.isEmpty(tmpMapVal))
	                    {
	                        System.out.println("added tempTem: >"+tempTem+"<");
	                        idMap.put(tempTem, "gotIt");
	                    }
	                }
	                 
	                lineIndex ++;
	            }
	             
	            w.write("【2014-01-07,2014-01-08,2014-01-09获取短信失败的用户ID列表】");
	            w.newLine();
	             
	            //w.flush();
	             
	            for (String key : idMap.keySet()) {
	                w.write(key);
	                w.newLine();
	                 
	                w.flush();
	            }
	             
	            w.close();
	            d.close();
	        }
	        catch (FileNotFoundException e)
	        {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	            return false;
	        }
	        catch (IOException e)
	        {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	            return false;
	        }
	        return true;
	    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值