Watching a Directory for Modifications in Java 7Posted: 03/20/2012 Filed under: Java 7 New Features | Tags: Directory, Java 7, Notification, Service, Watch, Watcher 2 Comments »
NIO 2.0 in Java 7 added support for directory change notification known as a watch service. It lets you receive notifications, when a directory is modified. There are six steps needed to set up a watch service.
- Creating a watch service
- Registering a directory with the watch service
- Retrieving a watch key from the watch service queue
- Processing the events that occurred on the registered directory
- Resetting the watch key after processing the events
- Closing the watch service
A watch service is an instance of the WatchService interface. You can create a watch service for the default file system as:
WatchService ws = FileSystems.getDefault().newWatchService();
Click here to read the full post in PDF format.