6

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

Community
  • 1
  • 1
ant
  • 22,634
  • 36
  • 132
  • 182
  • Dup of http://stackoverflow.com/questions/557844/java-io-implementation-of-unix-linux-tail-f – skaffman Feb 10 '10 at 16:24
  • @skaffman not a duplicate .. and I still didn't get my answer .. I need this to run constantly – ant Feb 10 '10 at 16:28
  • The accepted answer in the post linked by me and by skaffman does run continuously... – Alberto Zaccagni Feb 10 '10 at 16:37
  • @Montecristo Then its not duplicated question isn't it so? Because the server.log changes every few seconds .. I'd like to add those changed lines from server.long to my already outputted console output(at the end of the output) – ant Feb 10 '10 at 16:39
  • 1
    Are you sure that `while ((line = br.readLine()) != null)` is the same as doing `while (keepReading)` and after that reading/sleeping depending on what the `BufferedReader` read? I cannot try it by myself at the moment but I don't think it is... – Alberto Zaccagni Feb 10 '10 at 16:52
  • @Montecristo I'm not sure thats why I ask for help, if I had the slightest idea how to do this I'd do it on my own.. with pleasure .. but now I just sit, wait and refresh(or F5) – ant Feb 10 '10 at 16:54
  • Instead of hitting refresh I suggest you remove the `br.readLine()` from the while and put it in its body, as the example I linked shows – Alberto Zaccagni Feb 10 '10 at 16:56
  • @Montecristo what will I put inside while then if not that ? – ant Feb 10 '10 at 16:59
  • @Montecristo Of course you're not, I meant comparing to me :D I accepted & voted ur answer – ant Feb 10 '10 at 17:10
  • Please don't use DataInputStream to read text. Unfortunately examples like this get copied again and again so if you can remove it from your example. http://vanillajava.blogspot.co.uk/2012/08/java-memes-which-refuse-to-die.html – Peter Lawrey Jan 30 '13 at 23:39
  • Instead of waiting using sleep, you might want to check out the FileAlterationListener in Apache Commons IO library. – Jindra Helcl Jan 15 '15 at 10:46

2 Answers2

2

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

Community
  • 1
  • 1
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
1

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

}
}