Java - File Input Output Directory Watcher

Introduction

Java supports a watch service to notify a program when an object in a file system is modified.

The watch service uses the native file event notification facility of the file system.

The following classes and interfaces in the java.nio.file package are used in a watch service:

Interface
Meaning
Watchable


represents a file-system object that can be watched for changes.
A Watchable object can be registered with a watch service.
A Path object is a Watchable object.
WatchService

represents a watch service that watches registered objects for changes.
WatchService returns a WatchKey that serves as a token for the registration.
WatchKey
A WatchKey identifies the registration of an object with a WatchService.
WatchEvent<T>


A WatchEvent represents an event on an object registered with a watch service.
kind() method returns the kind of event occured.
context() method returns a Path object that represents the entry on which the event occurs.
WatchEvent.Kind<T>
represents the kind of event that occurs on a registered object.
StandardWatchEventKinds
defines constants to represent the kind of an event.

StandardWatchEventKinds class defines the following four constants to identify the kind of an event.

Each constant is of the type WatchEvent.Kind type. It contains the following constants:

  • ENTRY_CREATE
  • ENTRY_DELETE
  • ENTRY_MODIFY
  • OVERFLOW

The following code has a complete program that watches a C:\yourFolder directory for changes.

Demo

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class Main {
  public static void main(String[] args) {
    try (WatchService ws = FileSystems.getDefault().newWatchService()) {

      Path dirToWatch = Paths.get("C:\\");

      // Register the path with the watch service for create,
      // modify and delete events
      dirToWatch.register(ws, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);

      System.out.println("Watching " + dirToWatch + " for events.");

      // Keep watching for events on the dirToWatch
      while (true) {
        // Retrieve and remove the next available WatchKey
        WatchKey key = ws.take();

        for (WatchEvent<?> event : key.pollEvents()) {
          Kind<?> eventKind = event.kind();
          if (eventKind == OVERFLOW) {
            System.out.println("Event overflow occurred");
            continue;
          }//from  w w  w  . jav  a2 s.  c om

          WatchEvent<Path> currEvent = (WatchEvent<Path>) event;
          Path dirEntry = currEvent.context();

          // Print the event details
          System.out.println(eventKind + " occurred on " + dirEntry);
        }

        // Reset the key
        boolean isKeyValid = key.reset();

        if (!isKeyValid) {
          System.out.println("No longer watching " + dirToWatch);
          break;
        }
      }
    } catch (IOException | InterruptedException e) {
      e.printStackTrace();
    }
  }
}

Result