I recently implemented Java 7’s WatchService and it works perfectly. Now I wondered if there is a way to get all the Events which occured since the last run of my program. For example:
- I run my program, create some files, edit some files and I get all the corresponding Events
- I close my program
- I create a file named
foo.txt - I start my program, and the first event i get is an
ENTRY_CREATEforfoo.txt
I thought about saving the lastModifiedDate and searching for files and directorys newer than the last execution of my program. Is there another (and better) way to do this?
There is no better way to do this if your program is meant to scan for all file changes (apart from storing files in a content / source control repository, but that would be external to your program).
Java 7’s
WatchServiceis only a more performant way than continuously looping and comparing file dates / folder contents, hence you need to implement your own logic to solve your own problem.