public class Tail {
    public static void main(String[] args) throws IOException {
        Reader fileReader = new FileReader(args[0]);
        BufferedReader input = new BufferedReader(fileReader);
        String line = null;
        while (true) {
            if ((line = input.readLine()) != null) {
                System.out.println(line);
                continue;
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException x) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        input.close();
    }
}

Easy enough to be posted without any comment :-)


11 Responses to “Listening changes on a text file (Unix Tail implementation with Java)”  

  1. 1 Roger

    God, Have been looking everywhere for that!!!

    Geeze, everyone else’s is like 100000 lines of code, i can’t believe it was that simple.

    THANK YOUUUUUUU !!!!!!!

  2. 2 Gozala

    Thank you!!
    exactly what I was looking for!!

  3. 3 borras

    great !!!

  4. 4 hans

    great job.

  5. 5 Waste

    Not of any use.
    It just lists the contents of the given file once and keeps on running in a loop and does not shows the new lines added to the given file.

  6. 6 antonio

    hehehe. are you out of your mind?

  7. 7 Alessandro Melandri

    Great job! This saves me a lot of time! I’ve done some little customization to this class: http://www.melandri.net/java/tail

  8. 8 James

    Thank you!!!!!!!

  9. 9 Sanka

    It just simple does not work with log files rotated with log4j…I mean this code does prevent log4j from rotating !

  10. 10 Chamara Silva

    Great post. thank you.

    /Chamara Silva

  1. 1 Java tail | Alessandro Melandri


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Calendar

November 2005
M T W T F S S
« Oct   Dec »
 123456
78910111213
14151617181920
21222324252627
282930  

Follow me

twitter flickr LinkedIn feed

Subscribe by email

Enter your email address:

Archives


Categories

Tag Cloud


Listening