title: Read a log file line by line
body: <p>I am trying to read a file line by line and look out for lines that contain the string &quot;Inside /buyairtime&quot; in it, then get the AppSessionId from that line, after that use the AppSessionId to get the requestBody that should be in the next line and retrieve the transactionId.</p>
  <p>I am able to read the file and get the AppSessionId but I am not able to get the &quot;requestBody&quot;, please any help will be appreciated.</p>
  <p>here is what I have tried.</p>
  <pre class="lang-java prettyprint-override"><code>try(BufferedReader br = new BufferedReader(fileReader)) {
      br.lines().forEach((line) -&gt; {
  
          if (line.contains(&quot;Inside /buyairtime&quot;)) {
              String[] item = line.split(&quot;\\|&quot;);
              String AppSessionId = item[1].trim();
              logger.info(&quot;SessionId =&quot; + sessionId + &quot; AppSessionId =&quot; + AppSessionId);
  
              if (line.contains(AppSessionId) &amp;&amp; line.contains(&quot;requestBody&quot;)) {
                  String[] request = line.split(&quot;\\|&quot;);
                  String requestBody = request[2].replace(&quot;#&quot;, &quot;&quot;).trim();
                  logger.info(&quot;SessionId =&quot; + sessionId + &quot; requestBody =&quot; + requestBody);
              }
          }
      });
  
  } catch (IOException e) {
      e.printStackTrace();
  }
  </code></pre>
  <p>Ex: here is an example of lines to read</p>
  <pre><code>[http-nio-8090-exec-66] g.c.v.M.controller.ApiController         : AppSessionId | MB-9672588DE0304D7 | ######## Inside /buyairtime Route
  
  g.c.v.M.controller.ApiController         : AppSessionId | MB-9672588DE0304D7 | ######## requestBody = {&quot;amount&quot;:&quot;4.0&quot;,&quot;msisdn&quot;:&quot;233200906179&quot;,&quot;transactionid&quot;:&quot;0000001853861312&quot;}
  
  g.c.v.M.controller.ApiController         : AppSessionId | MB-9672588DE0304D7 | ######## Client IP = 127.0.0.1
  </code></pre>
  <p>here is an <a href="https://i.stack.imgur.com/r9w7t.jpg" rel="nofollow noreferrer">image</a> of the part of the file</p>