I'd like to be notified when a file has been changed in the file system. I have found nothing but a thread that polls the lastModified File property and clearly this solution is not optimal.
At the low level the only way to model this utility is to have a thread polling on a directory and keeping a watch on the attributes of the file. But you can use patterns to develop a adapter for such a utility.
For example j2ee application servers like Tomcat and others have a auto load feature where in as soon as deployment descriptor changes or servlet class changes the application restarts.
You can use the libraries from such servers as most of the code of tomcat is reusable and opensource.
Even that approach of polling last modified property is not foolproof. How would it handle a file restored from backup?
If you are willing to part with some money JNIWrapper is useful library with a Winpack, you will be able to get file system events on certain files. Unfortunately windows only.
http://www.teamdev.com/jniwrapper/index.jsf
Otherwise, resorting to native code is not always a bad thing especially when the best on offer is a polling mechanism as against a native event.
I've noticed that java file system operations can be slow on some computers and can easily affect the applications performance if not handled well.
I've written a log file monitor before, and I found that the impact on system performance of polling the attributes of a single file, a few times a second, is actually very small.
Java 7, as part of NIO.2 has added the WatchService API
The WatchService API is designed for applications that need to be notified about file change events.
Fair enough, I was confusing the matter with monitoring a large directory which I attempted and then resorted to the native option.
A single file shouldn't have that much impact.
"More NIO features" has file watch functionality, with implementation dependent upon the underlying OS. Should be in JDK7.
I use the VSF API from Apache Commons, here is an example of how to monitor a file without much impact in performance:
There is a cross-desktop library for files and folders watching called JxFileWatcher. It can be downloaded from here: http://www.teamdev.com/jxfilewatcher/
Also you can see it in action online: http://www.teamdev.com/jxfilewatcher/onlinedemo/
with implementation dependent upon the underlying OS. Should be in JDK7. thesis statement
You may also consider the Apache Commons JCI (Java Compiler Interface). Although this API seems to be focused on dynamic compilation of classes, it also includes classes in its API that monitors file changes.