About Me

B.E.(Computer Science), Android/Java Developer, CCNA, CCNA SECURITY (IINS), CCNP (R&S), 4011 Recognized(NSA & CNSS)U.S.A. , MCSA, MCTS, REDHAT CERTIFIED NETWORK SECURITY ADMINISTRATOR(RH253), AFCEH.

Friday, March 21, 2014

FileWatcher implementation in Java 7

       

 /**
  * This method handles the Watcher as the File is Modified .......... By Kumar Vivek Mitra
  * @param folderToBeWatched
  */
 public void fileModificationWatcher(final String folderToBeWatched){
  
  new Thread(new Runnable(){

   @Override
   public void run() {
    
    try {
       Path faxFolder = Paths.get(folderToBeWatched);
       watchServiceCreate = FileSystems.getDefault().newWatchService();
       faxFolder.register(watchServiceCreate, StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_CREATE);
      
       boolean valid = true;
       do {
        watchKeyCreate = watchServiceCreate.take();

         for (WatchEvent event : watchKeyCreate.pollEvents()) {
           WatchEvent.Kind kind = event.kind();
           if (StandardWatchEventKinds.ENTRY_MODIFY.equals(event.kind()) || StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind()) || StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) {
             String fileName = event.context().toString();
             makeChangesToHashFile(folderToBeWatched);
             System.out.println("File Modified:" + fileName);
           }else{
            System.out.println("Still not");
           }
           
         }
         
         valid = watchKeyCreate.reset();
         
                        
       }while(valid);
       
    } catch (Exception e) { e.printStackTrace(); }
    
   }
   
  }).start();
 }
 
 

      
 

No comments:

Post a Comment