Listen for changes to a text file

Hi all,

firstly I hope this is the right place for this post. 2ndly I hope I don't already have the answer, but here goes.

My program reads in a text file. At the moment I assume that it is static, but now I want to be able to listen for changes to that file (e.g. more lines added) and when they are do stuff.

I think I could do it by having a separate thread checking the number of lines in the file and reading any new ones, but really I'd like to have a change listener which raises an event when a line is added.

In this thread from 2003 it seems to be suggested that I have to have a separate thread checking the file for me, but I was wondering if anyone had any better solutions?

http://forum.java.sun.com/thread.jspa?forumID=256&threadID=443852

[796 byte] By [johnmcparlalda] at [2007-10-3 4:13:14]
# 1

Aside ffrom the timer solution which is simple and portable (100% Java), you have the system specific solution using JNI.

I've done that on Windows. Works very well.

Pro: Your program will be notified the instant a change is made to a file.

Big Con: The JNI solution is (clearly) not portable. If you're not familiar with C/C++ or if you have to support many OSes it could be a (big) problem.

AFAIK those are the two options you have.

Regards

jfbrierea at 2007-7-14 22:14:12 > top of Java-index,Core,Core APIs...
# 2
Thanks for the advice,the timer solution it has to be then - the reason I'm doing this in Java is for portability so that's my decision made then.Thanks,John
johnmcparlalda at 2007-7-14 22:14:12 > top of Java-index,Core,Core APIs...