views:

5563

answers:

11

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.

+3  A: 

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.

Rutesh Makhijani
A: 

Even that approach of polling last modified property is not foolproof. How would it handle a file restored from backup?

Conrad
I simply don't have that requirement :)
cheng81
+1  A: 

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.

Fearstruck
+10  A: 

There is a lib called jnotify that wraps inotify on linux and has also support for windows. Never used it and I don't know how good it is, but it's worth a try I'd say.

André
+2  A: 

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.

Stephen Denne
A: 

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.

Fearstruck
A: 

"More NIO features" has file watch functionality, with implementation dependent upon the underlying OS. Should be in JDK7.

Tom Hawtin - tackline
+6  A: 

I use the VSF API from Apache Commons, here is an example of how to monitor a file without much impact in performance:

DefaultFileMonitor

Telcontar
thanks for this answer! exactly what I searched for…
bene
A: 

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/

A: 

with implementation dependent upon the underlying OS. Should be in JDK7. thesis statement

JAck
A: 

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.

Example: http://commons.apache.org/jci/usage.html

jigjig