Since the Operating system is the second person after the person himself to know that a file is being created/deleted/modified residing in a file system. Is there by any means the OS will notify my application when there is any change (created/deleted/modified) to my application monitoring directory/file system ? I want to update my file in the application cache.. Previously i was polling to the directory at scheduled intervals to check for changes. but that seem to be a performance issue. So i came into this.. I don want to restrict with any OS as i always wanted to be Platform independent.
If there are any other solution which you feel better, you can tell me.
Santhosh Venkatesan wrote:I don want to restrict with any OS as i always wanted to be Platform independent.
Then you're out of luck, because this probably isn't just OS dependent - it's probably file system dependent. NTFS may support it, whereas FAT32 may not (to be honest, I have no idea). Either way, you will need to use JNI for this.
Something like this is supposed to be available in JDK 7 - if you're willing to wait, what, a year or so before it's officially released. Or if you're willing to build the OpenJDK releases yourself. The latter is not a bad option for developers, but may be a problem if you want to distribute the code to customers.
Joined: Mar 28, 2009
Thanks guys for your quick reply. I noticed something about JNotify. Have anyone used it? I will look into JNI as well..
if you are on windows, then you might have to hook certain windows messages (which windows always publish whenever handling with files or doing some specific IO, you have to digg into it) and to utilize this in java .. you have to write a JNI wrapper.
The difference between <b>failure</b> and <b>success</b> is often being <b>right</b> and being <b>exactly right</b>.
You may also wish to have a look at Apache Commons VFS - I found that it supports something called FileChangeEvent with ChangedEvent, CreateEvent and DeleteEvent as subclasses - seems like it's what you need. Though I haven't used it myself
Mike Simmons wrote:Something like this is supposed to be available in JDK 7
That's actually pretty cool, but it still uses polling if the file system does not support notifications.
Adam Michalik wrote:You may also wish to have a look at Apache Commons VFS - I found that it supports something called FileChangeEvent with ChangedEvent, CreateEvent and DeleteEvent as subclasses - seems like it's what you need. Though I haven't used it myself
That one also polls, something Santhosh is trying to avoid.
So the end conclusion is: it's possible for some file systems in native code, for others you need to poll.
Rob: yeah, it may revert to polling if there's no alternative, but that's the best you can do in the face of an impossible requirement, I think. It uses the OS facilities if they're available.
Henry's point is also good - in general, polling really isn't a problem. Unless perhaps you need to react to file changes within a few milliseconds, or unless you have a directory with a huge number of old entries that you have to filter out while looking only for the new ones. In the latter case, you probably need to modify the process to delete or move the already-processed files, so they don't gum up the works.