• Post Reply Bookmark Topic Watch Topic
  • New Topic

Detecting when a file is renamed

 
Jerry Duffy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

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.

Thanks in advance,
Jerry
 
Norm Radder
Bartender
Posts: 1526
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> new file that has the exact same size and modified date

That seems to be the only likely way.
Without being able to look under the covers to see where the extents of the file are on disk, I don't see any other way.
 
Jerry Duffy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was the conclusion I reached a couple of days ago, but I thought I'd take a shot at asking anyway. Thanks for the reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!