Search code examples
javafilesystemwatcherjava-7

Java monitor file system when java is not running


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:

  1. I run my program, create some files, edit some files and I get all the corresponding Events
  2. I close my program
  3. I create a file named foo.txt
  4. I start my program, and the first event i get is an ENTRY_CREATE for foo.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?


Solution

  • 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 WatchService is 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.