Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continually read the lines being appended to a log file

Tags:

java

Concerning my previous question , 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:

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.

What I actually need is:

  1. read file server.log
  2. when server.log is changed with adding few more lines output the change

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:

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());
    }
  }
}

According to the Montecristo suggestion I did this :

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());
        }
    }
}

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.

HERE IS THE SOLUTION: tnx Montecristo

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());
        }
    }
}

Also see :

http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html

like image 398
ant Avatar asked Feb 10 '10 16:02

ant


People also ask

How to read a file continuously in java?

Instead, try doing a while(true){ } loop.

How to write log into text file in c#?

C# write text with StreamWriter's WriteLine txt"; using var sw = new StreamWriter(path); sw. WriteLine("old falcon"); Console. WriteLine("data written to file"); The example writes one line to the text file.


2 Answers

I don't know if you're going in the right direction but if I've understood correctly you'll find this useful: java-io-implementation-of-unix-linux-tail-f

like image 171
Alberto Zaccagni Avatar answered Oct 20 '22 09:10

Alberto Zaccagni


You can use RandomAccessFile.

import java.io.IOException;
import java.io.RandomAccessFile;

public class LogFileReader {

    public static void main( String[] args ) {
       String fileName = "abc.txt";
       try {
        RandomAccessFile bufferedReader = new RandomAccessFile( fileName, "r" 
        );

        long filePointer;
        while ( true ) {
            final String string = bufferedReader.readLine();

            if ( string != null )
                System.out.println( string );
            else {
                filePointer = bufferedReader.getFilePointer();
                bufferedReader.close();
                Thread.sleep( 2500 );
                bufferedReader = new RandomAccessFile( fileName, "r" );
                bufferedReader.seek( filePointer );
            }

        }
    } catch ( IOException | InterruptedException e ) {
        e.printStackTrace();
    }

}
}    
like image 37
Shilkumar Jadhav Avatar answered Oct 20 '22 08:10

Shilkumar Jadhav