• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Detecting when a file is renamed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 4888
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic