title: Continually read the lines being appended to a log file
body: <p>Concerning my previous <a href="https://stackoverflow.com/questions/2237202/how-can-i-see-jboss-console-with-maven/2238061">question</a> , I found out that maven can't really output jboss console. So I thought I'd like to make workaround it. Here is the deal:</p>
  
  <p>While jboss is running, it writes console logs into server.log file, so I'm trying to retrieve the data as it comes in, because every few seconds the file is changes/updated by jboss I've encountered some difficulties so I need help.</p>
  
  <p>What I actually need is:</p>
  
  <ol>
  <li>read file server.log</li>
  <li>when server.log is changed with adding few more lines output the change</li>
  </ol>
  
  <p>Here is the code so far I got, there is a problem with it, it runs indefinitely and it starts every time from the beginning of the file, I'd like it to continue printing just the new lines from server.log. Hope it makes some sense here is the code:</p>
  
  <pre><code>import java.io.*;
  
  
  class FileRead 
  {
     public static void main(String args[])
    {
        try{
     for(;;){ //run indefinitely
      // Open the file 
      FileInputStream fstream = new FileInputStream("C:\\jboss-5.1.0.GA\\server\\default\\log\\server.log");
      BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
      String strLine;
      //Read File Line By Line
      while ((strLine = br.readLine()) != null)   {
        // Print the content on the console
        System.out.println (strLine);
      }
      //Close the input stream
      in.close();
      }
    }
        catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
      }
    }
  }
  </code></pre>
  
  <p>According to the Montecristo suggestion I did this :</p>
  
  <pre><code>import java.io.*;
  
  class FileRead {
      public static void main(String args[]) {
          try {
              // Open the file that is the first
              // command line parameter
              FileInputStream fstream = new FileInputStream(
                      "C:\\jboss-5.1.0.GA\\server\\default\\log\\server.log");
              BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
              String line;
              // Read File Line By Line
              while ((line = br.readLine()) != null) {
                  // Print the content on the console
                  line = br.readLine();
                  if (line == null) {
                      Thread.sleep(1000);
                  } else {
                      System.out.println(line);
                  }
  
              }
              // Close the input stream
              in.close();
  
          } catch (Exception e) {// Catch exception if any
              System.err.println("Error: " + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>And it still not working, it just printed the original file.. although the file changes constantly nothing happens.. nothing gets printed out except the original log file.</p>
  
  <p><strong>HERE IS THE SOLUTION:</strong> tnx <a href="https://stackoverflow.com/users/57095/montecristo">Montecristo</a></p>
  
  <pre><code>import java.io.*;
  
  class FileRead {
      public static void main(String args[]) {
          try {
  
              FileInputStream fstream = new FileInputStream(
                      "C:\\jboss-5.1.0.GA\\server\\default\\log\\server.log");
  
              BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
              String line;
  
              while (true) {
  
                  line = br.readLine();
                  if (line == null) {
                      Thread.sleep(500);
                  } else {
                      System.out.println(line);
                  }
  
              }
  
          } catch (Exception e) {
              System.err.println("Error: " + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>Also see :</p>
  
  <p><a href="http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html" rel="nofollow noreferrer">http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html</a></p>