Watching files in Java with WatchService

Java 7 introduced a useful API for watching files on the file system. This feature comes in handy if you need to scan a directory for new files or modified files. The API is event driven, so the system will notify you of certain events such as “new file”, “modified file” or “deleted file”.

The example below demonstrates the key features of the WatchService API. It scans a given directory for file events and calls the appropriate handle methods. Inside of the handler methods, you can provide your own custom handling. It is important to note that a separate event is generated for each file. So for example, if you add 10 new files to the directory, then it will generate 10 new events.


Did you like my blog post?

Signup now and receive an email when I publish new luv2code content.

I agree to have my personal information transfered to AWeber ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Leave a Reply