I am developing a File Monitoring component. Currently, the component can detect when a File object (system file or directory) has changed in someway, by using the lastModified() method of the File object. Now, I want to extend the functionality of the component, and identify exactly what changed.
I'm running into problems detecting a file that is renamed. When a rename operation occurs, I get notification that a change occurred, but it looks like a delete rather than a rename. I've run some tests on the File object, and this is the behavior I'm seeing:
Of course, this makes perfect sense, since the String passed to the File constructor represents a specific file on the file system, and once that file gets renamed, that specific file doesn't "exist" anymore (at least under that name).
I'm writing this under Win2000, which doesn't update the last modified date when a file is renamed (maybe unix handles this differently).
So, to make a long story short: Is there a way to detect when a file has been renamed using 100% java (no JNI)?
I've considered doing some sort of "snapshot" of the parent directory, and when a file is "deleted", look for a new file that has the exact same size and modified date as the deleted file. Of course, the behavior might be platform dependent, and performance would be horrible for directories that contain large numbers of files.