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.
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.
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