Click here to Skip to main content
13,295,429 members (49,044 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 12 Mar 2013

File Change Notification in Java 7

, 12 Mar 2013
Rate this:
Please Sign up or sign in to vote.
This article describes the File Change Notification functionality in Java 7.

Sample Image


File Change Notification is a new feature added in JDK 7. It is a part of the new file system API (NIO 2.0). It uses the WatchService API to receive file change notification events like creation, modification, and deletion of files or directories.


Following are the steps required to monitor the notification events:

  • Create a WatchService as follows:
  • WatchService service = FileSystems.getDefault().newWatchService();
  • Get a reference of the file or directory to be monitored as follows:
  • Path path = Paths.get(dir);
  • Register the file/directory with the WatchService, specifying the events to be monitored (Create, Modify, Delete) as follows:
  • path.register(watchService,StandardWatchEventKinds.ENTRY_CREATE,
  • Create an infinite loop to monitor the events using the take() method to retrieve a watchkey placed into the watch service's queue when an event occurs as follows:
  • while(true)
        WatchKey key = service.take();
  • Inside the infinite loop, use another loop to print all events as follows: If the directory becomes inaccessible, the key will become invalid and the loop will be exitted.
  • for (WatchEvent event : key.pollEvents())
         System.out.println(event.kind() + ": "+ event.context());
    boolean valid = key.reset();
    if (!valid)

Using the code

Following is the complete java code:

import java.nio.*;
import java.nio.file.StandardWatchEventKinds.*;
import java.nio.file.*;

public class FileChangeNotifier
   public static void main(String args[]) throws IOException, InterruptedException
      watchDir("E:\\MyFolder");		// Monitor changes to the files in E:\MyFolder
   public static void watchDir(String dir) throws IOException, InterruptedException
       WatchService service = FileSystems.getDefault().newWatchService();	// Create a WatchService
       Path path = Paths.get(dir);	// Get the directory to be monitored
            StandardWatchEventKinds.ENTRY_DELETE);	// Register the directory
          WatchKey key = service.take();	// retrieve the watchkey
          for (WatchEvent event : key.pollEvents())
             System.out.println(event.kind() + ": "+ event.context());	// Display event and file name
          boolean valid = key.reset();
          if (!valid)
             break;	// Exit if directory is deleted

Points of Interest

The File Change Notification feature provides a simple and elegant way of monitoring file notification events.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Azim Zahir
Instructor / Trainer NIIT, India
India India
I am a trainer by profession. Currently I am working with NIIT (Mumbai, India) as a Senior Faculty. I enjoy programming as a hobby. My favorite technologies are Flash, Flex and Silverlight.

Of late I have developed keen interest in WPF and Windows Mobile programming.

Apart from computers, my favorite pastime is bicycling.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 12 Mar 2013
Article Copyright 2013 by Azim Zahir
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid