Java - 如何只读取输入流的某些部分

时间:2011-04-13 18:25:11

标签: java file io

我有一个要求,我需要解析输入文件并只读取文件的某些部分。我有一个日志文件,它有不同的级别,如信息警告和错误。现在我只需要读取包含完整错误堆栈跟踪的部分。我如何使用java实现这一目标。

例如:

INFO  | 2011-04-13 17:59:22,810 | Calling Feedback from 127.0.0.1 
INFO  | 2011-04-13 17:59:24,920 | Successfully called Feedback from 127.0.0.1
INFO  | 2011-04-13 17:59:31,561 | FeedBackList

ERROR | 2011-04-13 19:00:41,640 |  
java.util.concurrent.TimeoutException
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:228)
    at java.util.concurrent.FutureTask.get(FutureTask.java:91)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:111)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy309.getConsumerProfileData(Unknown Source)
    at com.scea.usps.model.service.impl.AccountSettingsServiceImpl.getUserProfile(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.orm.hibernate3.HibernateInterceptor.invoke(HibernateInterceptor.java:111)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy284.getUserProfile(Unknown Source)
    at com.scea.usps.model.common.PsninfoUtility.getTop3Generes(Unknown Source)
    at com.scea.usps.model.common.PsninfoUtility.updatePsnInfoDetail(Unknown Source)
    at com.scea.platform.framework.api.PsnInfoThread.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:619)

INFO  | 2011-04-13 17:59:22,810 | Calling Feedback from 127.0.0.1 
INFO  | 2011-04-13 17:59:24,920 | Successfully called Feedback from 127.0.0.1
INFO  | 2011-04-13 17:59:31,561 | FeedBackList

在上面的日志中,我需要从ERROR开始提取(读取)所有行,直到堆栈跟踪结束。请分享您的想法。感谢。

5 个答案:

答案 0 :(得分:2)

BufferedReader in = new BufferedReader(new FileReader("logfile.log"));
  String line = in.readLine();
  StringBuffer buf = null;
  while (line != null) {
    if(line.startsWith("ERROR")){
       buf = new StringBuffer();
       buf.append(line).append("\n");
       while(line != null && !line.trim().equals("")){
          line = in.readLine();
          buf.append(line).append("\n");
       }
       //Now buf has your error an do whatever you want to do with it
       //then delete
       buf = null;
    }
    line = in.readLine();
  }

答案 1 :(得分:1)

如果你的目标是观察应用程序的日志中的错误,那么像Chainsaw这样的工具可能是更好的解决方案。

答案 2 :(得分:1)

如果使用LogFilePatternReceiver,Chainsaw确实有这个内置功能。您可以定义filterExpression,并且只处理与该filterexpression匹配的事件。

仅包含堆栈跟踪的示例过滤器表达式将是:

存在异常

您必须提供日志文件的格式。有关详细信息,请参阅JavaDoc:

http://logging.apache.org/log4j/companions/receivers/apidocs/org/apache/log4j/varia/LogFilePatternReceiver.html

答案 3 :(得分:0)

  • 打开文件
  • 阅读直到您使用ERROR
  • 读取并处理行,直到在堆栈跟踪后点击空白行。

冲洗,重复。

答案 4 :(得分:0)

try {
      BufferedReader in
          = new BufferedReader(new FileReader("logfile.log"));
      String line = in.readLine();
      while (!line.startsWith("ERROR")) {
        line = in.readLine();
        if(line==null){
          //throw exception here, ERROR not found in entire log file
        }
      }
      //HERE line will be your error line
      while (line!=null) {
        line = in.readLine();
        //do something with line
      }
      //here you have reached the end of the file
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }