WatchService in Java 7

                                                File change notifications

In JDK 7 release , the addition of File Change Notifications finally carved into NIO 2.0.The WatchService API lets you receive notification events upon changes in file under a Directory  i.e on creation, modification and deletion of file.

There are 3 events to watch for:

ENTRY_CREATE
ENTRY_DELETE
ENTRY_MODIFY
The steps involved are:

i) Create a WatchService object which consists of a queue to hold WatchKeys

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

ii) Get Path to the watchable directory

Path path= Paths.get(“C:\\User\\code\\watchservice\\”);

iii) We need to register the directory to this WatchService and need to specify the types of events

path.register((watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);

iv) Start a loop to listen to Events and Iterate through the keys

WatchKey key = watchService.take(); // this will return the keys

v) We will then call the pollEvents method to retrieve all the events for this WatchKey object

WatchEvent<?> event : key.pollEvents()

vi) Returns the event type

WatchEvent.Kind<Path> kind = watchEvent.kind();

vii) The reset method sets the WatchKey state back to “ready” and returns a boolean indicating if the WatchKey is still valid or not.This is the most import part of Watch Service functionality.

key.reset();

 

CODE:

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.StandardWatchEventKinds;

public class WatchServiceJava7Feature {

public static void main(String[] args) throws Exception {
WatchService watchService = FileSystems.getDefault().newWatchService();
Path path= Paths.get(“C:\\User\\code\\watchservice\\”);
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);
while(true)
{
WatchKey key = watchService.take(); // this will return the keys
for(WatchEvent<?> event : key.pollEvents())
{
WatchEvent<Path> watchEvent = (WatchEvent<Path>) event;
WatchEvent.Kind<Path> kind = watchEvent.kind();
switch(kind.name()) {
case “ENTRY_MODIFY”:
System.out.println(“Case Modify :Event on “+ event.context().toString() + ” is “+ kind);
break;
case “ENTRY_DELETE”:
System.out.println(“Case Delete :Event on “+ event.context().toString() + ” is “+ kind);
break;
case “ENTRY_CREATE”:
System.out.println(“Case Create :Event on “+ event.context().toString() + ” is “+ kind);
break;
}

}
key.reset();
}

}}

 

OUTPUT:

Case Create :Event on New Text Document.txt is ENTRY_CREATE
Case Delete :Event on New Text Document.txt is ENTRY_DELETE
Case Modify :Event on New Text Document (2).txt is ENTRY_MODIFY

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s