Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: File Renaming Detection

Is there a way to detect file renaming using Java? (NIO's WatchService API or any other) From what I've seen when renaming a file two separate events occur - ENTRY_DELETE and then ENTRY_CREATE.

like image 449
Karoo Avatar asked Aug 17 '11 11:08

Karoo


3 Answers

Take a look at jpathwatch. In addition to the standard watch event kinds specified in Java 7 (ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY) it also has additional events called ENTRY_RENAME_FROM and ENTRY_RENAME_TO which will fire when a file is renamed. Note that this depends on whether the underlying operating system supports file renaming events or not.

like image 149
dogbane Avatar answered Oct 23 '22 13:10

dogbane


Detecting renaming of files can not be done portably. Because, surprising as it may seem, the idea that a file HAS-A name is not portable. Most Unix file systems allow a file to have any number of ASSOCIATED names (including zero names). In the Unix (POSIX) world, renaming means adding a new name and then removing the original name.

like image 28
Raedwald Avatar answered Oct 23 '22 12:10

Raedwald


I think the jnotify would be useful in your case.

like image 2
aphex Avatar answered Oct 23 '22 11:10

aphex