I am working on a software which monitors any modification in file and based on that performs some business logic. To implement the same, I am using Watch Service in Java 7 (http://docs.oracle.com/javase/tutorial/essential/io/notification.html ). However, using the code mentioned in above link, my software always receive File- Modification events, even when I am just saving the file, there is nothing changed in the file.
Ideally, my software should receive Modification events only when file is actually changed, not every time some-one just save the file. I also tried with JNotify-API and results where the same.
Any help or suggestions for the same is appreciated
Yeah, unfortunately we don't really have any way of knowing whether something was changed or not during a save, without examining the file contents. That info simply isn't provided. If it was me, I would probably create a Map or DB table of file paths and their file lengths and some hash of the file. If the length changes, you know there's a change; otherwise you still need to examine bytes to see if the content changed. I would calculate a new hash of the content and see if that hash changed - if it did, you definitely have a file change. If the hash didn't change, then you probably don't have any file change. But you can't be completely sure without doing a byte-by-byte comparison.
Joined: Jun 24, 2010
Thanks for the Reply and Suggestions.
I tried the same sample in Mac-OS, and it works fine. The software only receives events when file is actually changed. It seems that it depends upon underlying platform.
Saving a file normally changes its modification date, even if the data itself is unchanged. So by that metric, the file has "changed".
If you want to verify that a file's contents are unchanged, the typical approach is to take a checksum (usually MD5) of the original file and compare it with the checksum of the file after you got the change notification. That process can be speeded up by first checking metadata such as the file's length, since obviously a change in length indicates a change in content and that way you have an instant answer without the work of computing the new checksum.
Customer surveys are for companies who didn't pay proper attention to begin with.