WatchService in Java with example program

WatchService in Java with example program

Java 7 added a new feature to its NIO package called Watch Service API which allows applications to monitor a directory for changes. This allows, for example, a file manager to update its display of the list of files when files are created or deleted or modified. All interfaces and classes in the Watch Service API is present in the package java.nio.file. In this article we will see how to use WatchService in Java with example program.

Watch Service API

The java.nio.file package provides a file change notification API, called the Watch Service API. This API enables you to register a directory (or directories) with the watch service. When registering, you tell the service which types of events you are interested in: file creation, file deletion, or file modification. When the service detects an (registered)event, it is forwarded to the registered process. The registered process has a thread (or a pool of threads) dedicated to watching for any events it has registered for. When an event comes in, it is handled as needed.

Steps to implement a watch service

Here are the basic steps required to implement a watch service:

  • Create a WatchService “watcher” for the file system.
  • For each directory that you want to monitor, register it with the watcher. When registering a directory, you specify the type of events for which you want notification. The event can be create, delete or modify. You receive a WatchKey instance for each directory that you register.
  • Implement an infinite loop to wait for incoming events. When an event occurs, the key is signaled and placed into the watcher’s queue.
  • Retrieve the key (WatchKey) from the watcher’s queue. You can obtain the file name from the key.
  • Retrieve each pending event for the key (there might be multiple events) and process as needed.
  • Reset the key, and resume waiting for events. It’s very important that the key must be reset after the events have been processed. If not, the key won’t receive further events. If the reset() method returns false, the directory is inaccessible, so you may exit the loop. The key remains valid until: It is cancelled, explicitly, by invoking its cancel method, or cancelled implicitly, because the object is no longer accessible, or by closing the watch service.
  • Close the service: The watch service exits when either the thread exits or when it is closed (by invoking its closed method).

Note: The Watch Service API does not allow registering an individual file. You will get a java.nio.file.NotDirectoryException, if you are trying to do so.

Let us see a simple Java program with all the steps mentioned above.

Java program to monitor a directory using WatchService API

Create a folder named WatchFolder in D drive. Run the above program. Now, create a new file named sample.txt in that folder, then modify the contents of the file and then delete the file. The program would produce the below output.

The program waits infinitely. Kill the process or delete the folder to stop the program.

When to Use and Not Use This API

The Watch Service API is designed for applications that need to be notified about file change events. It is well suited for any application, like an editor or IDE, that potentially has many open files and needs to ensure that the files are synchronized with the file system. It is also well suited for an application server that watches a directory, perhaps waiting for .jsp or .jar files to drop, in order to deploy them.

This API is not designed for indexing a hard drive. Most file system implementations have native support for file change notification. The Watch Service API takes advantage of this support where available. However, when a file system does not support this mechanism, the Watch Service will poll the file system, waiting for events.

That’s all about Watch service API in Java. If you have any doubt, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.