Java NIO2 - Watching a directory for changes. FileWatcherService.

File Watch Service

The NIO2 WatchService API provides the ability for java code to watch for file modifications in a directory in the filesystem. A WatchService object watches the folder it is registered with and signals the changes detected through event notifications. 

Steps to create a WatchService

  1. Create a WatchService object (FileSystem.newWatchService())
  2. Register the directories to be watched with the WatchService (WatchService.register())
  3. Watch for file change events (poll()/take() methods)
  4. Retrieve the event keys and process them
  5. Rest the key and got to step 3 to watch again.

Example code

Below is a simple working code.
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class FileWatcherTest {
 
 private static void watchDir(Path dirPath) throws IOException, InterruptedException{
  
  WatchService watchService = dirPath.getFileSystem().newWatchService();
  dirPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY);
  for(;;){
   WatchKey key = watchService.take(); 
   //WatchKey key = watchService.poll(25,TimeUnit.SECONDS);
   System.out.println("watch at :" + new java.util.Date() + " - key :" + key);
   if(key == null) continue;
   List<watchevent> events = key.pollEvents();
   for(WatchEvent event : events){
    System.out.println("Event :" + event.kind() + " " + event.context().toString());
   }
   if(!key.reset()){
    System.out.println("Breaking ...");
    break;
   }
  }
 }
 
 public static void main(String[] args) throws IOException, InterruptedException {
  
  Path path =  Paths.get("C:\\watchtest");
  watchDir(path);
  
 }

}

A sample run gives following output.

Comments

  1. Your post is really awesome. It is very helpful for me to develop my skills in a right way.keep sharing such a worthy information
    salesforce Training in Bangalore
    uipath Training in Bangalore
    blueprism Training in Bangalore

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted.By Learn Digital Marketing Course Training in Chennai it will help to get Digital Marketing Course Training in Chennai.Learn Best Digital Marketing Course Training in Chennai's professional institute to get reputed job.

    ReplyDelete
  4. Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging...Well written article

    aws Training in Bangalore
    python Training in Bangalore
    hadoop Training in Bangalore
    angular js Training in Bangalore
    bigdata analytics Training in Bangalore

    ReplyDelete
  5. I appreciate your hard work and thanks for sharing. Thanks a lot for explaining practically. Fantastic Post! If you want to grow your Business Online, For more information, please visit our website:Digital Marketing Company in Ghaziabad
    SMM Services,
    PPC Services,
    SEM Services,
    Best Web Designing Company in India

    ReplyDelete
  6. Incredibly in general very intriguing post. I was looking for such an information and took pleasure in scrutinizing this one. Keep posting. An obligation of appreciation is all together for sharing.data analytics course in Hyderabad

    ReplyDelete

  7. This Blog have relevant information’s and reference links which not get bored to the readers.
    Ethical Hacking Course in Chennai
    Ethical Hacking course in Bangalore

    ReplyDelete
  8. Great blog!!! The information was more useful for us... Thanks for sharing with us...
    Python Classes in Chennai
    learn python online
    Python Classes in Coimbatore

    ReplyDelete
  9. Thanks for sharing a good article with us. It is very helpful to us, if you want to know more about accounting software our website is helpful to you...
    Accounting Software Singapore
    PSG Grant Accounting Software
    E invoicing Singapore

    ReplyDelete
  10. THANK YOU for this amazing and for sharing this blog with us, it is very helpful.
    please keep updated us more about like this type of blog.
    If someone is looking for the best java training institute for software training in Ghaziabad, java training institute
    It is the best place from where you get the practical knowledge of java training institute here. You will be an expert in this field after doing the java training.

    ReplyDelete

Post a Comment

Popular posts from this blog

SVN File Creation Date

Ubuntu / kubuntu - Laptop key board cursor jumping issue