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

| 我有一个需要解析输入文件并仅读取文件某些部分的要求。我有一个日志文件,其中包含不同级别的信息警告和错误。现在,我只需要阅读包含完整错误堆栈跟踪的部分。我如何使用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开始的所有行,直到堆栈跟踪结束。请就此分享您的想法。谢谢。     
已邀请:
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();
  }
    
如果您的目标是查看应用程序的日志中是否有错误,那么像电锯这样的工具可能是一个更好的解决方案。     
如果使用LogFilePatternReceiver,则电锯确实具有此内置功能。您可以定义filterExpression,只有与之匹配的事件将被处理。 仅包含堆栈跟踪的示例过滤器表达式为: 存在异常 您必须提供日志文件的格式。有关更多信息,请参见JavaDoc: http://logging.apache.org/log4j/companions/receivers/apidocs/org/apache/log4j/varia/LogFilePatternReceiver.html     
打开文件 读取直到出现错误为止 读取并处理行,直到在堆栈跟踪之后打到空白行。 冲洗,重复。     
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();
    }
  }
    

要回复问题请先登录注册