Open In App

How to Watch a Directory for Changes in Java ?

In this article, we will learn how to watch the directory changes through the Java programming language. Java provides a robust and efficient way to monitor directories for changes through the java.nio.file package. The WatchService API allows developers to receive notifications about various events such as file creation, deletion, and modification within a specific directory.

Import Required Packages

The WatchService interface is available in the java.nio.file package. We can use the WatchService interface using this file extension.



Package:

import java.nio.file.* 

Create the instance of WatchService

We can use the WatchService features. By creating the instance of the WatchService instance the java.nio.file File System class.



WatchService watchService = FileSystems.getDefault().newWatchService()

Steps for Implementation

Example to Watch a Directory for Changes in Java

The following program demonstrates the current directory changes using the WatchService.




// Java Program
import java.io.IOException;
import java.nio.file.*;
 
// Driver Class
public class FileData {
    // Main Function
    public static void main(String[] args) {
        try {
            // Specify the directory which supposed to be watched
            Path directoryPath = Paths.get("./");
 
            // Create a WatchService
            WatchService watchService = FileSystems.getDefault().newWatchService();
 
            // Register the directory for specific events
            directoryPath.register(watchService,
                    StandardWatchEventKinds.ENTRY_CREATE,
                    StandardWatchEventKinds.ENTRY_DELETE,
                    StandardWatchEventKinds.ENTRY_MODIFY);
 
            System.out.println("Watching directory: " + directoryPath);
 
            // Infinite loop to continuously watch for events
            while (true) {
                WatchKey key = watchService.take();
 
                for (WatchEvent<?> event : key.pollEvents())
                {
                    // Handle the specific event
                    if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE)
                    {
                        System.out.println("File created: " + event.context());
                    }
                    else if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE)
                    {
                        System.out.println("File deleted: " + event.context());
                    }
                    else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY)
                    {
                        System.out.println("File modified: " + event.context());
                    }
                }
 
                // To receive further events, reset the key
                key.reset();
            }
 
        }
        catch (IOException | InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}

Output:

Below is the output video for better understanding of the result.

StandardWatchEventKinds


Article Tags :