views:

75

answers:

2

What is the most efficient way to detect modification to a file in Java?

I've read about file polling however, the polling approach has a number of drawbacks that become apparent as the number of watched files increases. I rather not use polling but a number of solutions online appear to point to file polling as the recommended method. I was hoping for perhaps an OS-related file system update callback solution instead -- is this possible in both linux and windows?

Given that I currently only intend to 'watch' 2 files, I'm still kind of open to a file polling solution. Ideally, I'm hoping file polling won't have a great impact on performance.

I'm aware of Java 7's WatchService but since 7 hasn't been officially released, I'm hesitant to use that approach or JNI libraries.

Much appreciated!

EDIT: It appear file polling is the way to go. If anyone can recommend the most safest/efficient way to implement file polling, that would be great.

+1  A: 

There is no Java solution to have the OS notify you when a file changes. It may be possible through native calls, but each OS would be different in that regard, and it certainly isn't supported in Java. It would require a lot of custom coding with JNI - if it's even possible.

Going with the polling solution, as long as you poll for date modified, it should be pretty quick. Just be sure to not leave references to files open for extended periods of time. Always close them between polling cycles to ensure that you don't create conflicts.

Erick Robertson
True. All OS-related file system update callback solution that I've read about all point to JNI, which I'm also adversed to using. I guess file polling it is! :) Now to find the best solution to do so.
Personally, I would just write something short, simple, and to the point that did exactly what I needed. I wouldn't be surprised if jpoller worked, which @Brent suggested. jnotify looks like it needs additional libraries installed, and I don't like Java solutions that require that.
Erick Robertson
"be sure to not leave references to files open for extended periods of time." What do you mean by closing them between polling cycles to avoid conflicts? I was thinking of keeping my file polling active at all times because there is no telling when the user may want to alter the file, hence the listener must be ready to detect such activity at all times. When will the polling cycles conflict if there is one instance open at all times? Thanks! :)
Each polling cycle you should be sure to close all of your open file handles when you're done with the polling. So if you open a file for any reason, you have to close it. Don't leave it open for the 30 seconds until the next polling cycle. (or however long it is)
Erick Robertson
Oh I see, close every file that was opened during each polling interval no matter how short the interval is. I am polling each second to check the last modified time of the file. It seems to be working so far. Hopefully no performance issues as I go along. Thanks for all your help!
A: 

Here are two avaliable libraries:

Java 7 NIO2 might also have some things for you ( I dont know if thease features are in scope for Java 7 at the moment) Also avaliable as JSR 203 see here

oluies