Log File Tailer (tail -f) implementation in Java – Best way to tail any file programmatically

Log File Tailer Example in Java

File operations using Java are my favorite part. There are number of tutorials you may have seen on Crunchify like append data to file, read data from file, read and parse JSON file, read config.properties file during server startup, etc.

In this tutorial we will go over steps on how to implement linux tail -f command in Java.

Let’s get started:

  1. Create class CrunchifyTailFOnLogFile_LogFileTailer.java
  2. From main method start executor service to start log file tailer, i.e. crunchifyExecutor.execute(crunchify_tailF); which internally calls run()
  3. Also call appendData() method which will add new line to file every 5 seconds
  4. Once new line will be added to file, tailer will pick and print it to Eclipse Console

Output:

Have anything to add to this article? Please chime in and join the conversion.