Java IO implementation of unix/linux "tail -f"

Solution 1:

Take a look at Apache Commons implementation of Tailer class. It does seem to handle log rotation as well.

Solution 2:

The ability to continue to read a file, and wait around until the file has some more updates for you shouldn't be that hard to accomplish in code yourself. Here's some pseudo-code:

BufferedReader br = new BufferedReader(...);
String line;
while (keepReading) {
    line = reader.readLine();
    if (line == null) {
        //wait until there is more of the file for us to read
        Thread.sleep(1000);
    }
    else {
        //do something interesting with the line
    }
}

I would assume that you would want to put this type of functionality in its own Thread, so that you can sleep it and not affect any other areas of your application. You would want to expose keepReading in a setter so that your main class / other parts of the application can safely shut the thread down without any other headaches, simply by calling stopReading() or something similar.

Solution 3:

Check JLogTailer, which does this logic.

The main point in the code is:

public void run() {
    try {
        while (_running) {
            Thread.sleep(_updateInterval);
            long len = _file.length();
            if (len < _filePointer) {
                // Log must have been jibbled or deleted.
                this.appendMessage("Log file was reset. Restarting logging from start of file.");
                _filePointer = len;
            }
            else if (len > _filePointer) {
                // File must have had something added to it!
                RandomAccessFile raf = new RandomAccessFile(_file, "r");
                raf.seek(_filePointer);
                String line = null;
                while ((line = raf.readLine()) != null) {
                    this.appendLine(line);
                }
                _filePointer = raf.getFilePointer();
                raf.close();
            }
        }
    }
    catch (Exception e) {
        this.appendMessage("Fatal error reading log file, log tailing has stopped.");
    }
    // dispose();
}

Solution 4:

I've built a short implementation of "tail -f" in Scala some time ago: tailf. It takes care of file rotation as well and you may define your own logic what to do when it reaches EOF or finds the file has been renamed.

You may take a look and port it to Java, since actually there is nothing complex in there. Few notes: the main file is Tail.scala and basically it defines FollowingInputStream which takes care of EOF/rename and follow method, which wraps FollowingInputStream into an unbounded enumeration in SequenceInputStream. So, as soon as FollowingInputStream ends, SequenceInputStream requests next element from an Enumeration and another FollowingInputStream gets created.

Solution 5:

I stumbled recently over rxjava-file, It is an extension of RxJava. In contrast to the other solutions this makes use of Java's NIO.

import rx.Observable;
import rx.functions.Action1;
import com.github.davidmoten.rx.FileObservable;

// ... class definition omitted

public void tailLogFile() throws InterruptedException {
    Observable<String> tailer = FileObservable.tailer()
                                .file("application.log") // absolute path
                                .tailText();

    tailer.subscribe(
        new Action1<String>() {
            @Override
            public void call(String line) {
                System.out.println("you got line: " + line);
            }
        },
        new Action1<Throwable>() {
            @Override
            public void call(Throwable e) {
                System.out.println("you got error: " + e);
                e.printStackTrace();
            }
        }
    );

// this solution operates threaded, so something  
// is required that prevents premature termination

    Thread.sleep(120000);
}