Imaginsystems


Tecniche di Programmazione - Codici Sorgenti - News Informatiche
Archivio Posts
Anno 2014

Anno 2013

Anno 2012
Statistiche
  • Views Home Page: 58.223
  • Views Posts: 445.022
  • Views Gallerie: 0
  • n° Posts: 210
  • n° Commenti: 224

JAVA - Come monitorare una cartella (Codice)

JAVA - Come monitorare una cartella (Codice)


 

Come monitorare una cartella e stampare a video dei messaggi :

Created
Delete
Modify
CODICE JAVA :
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;

public class pp {

	public static boolean bandiera = true;
	
    public static void main(String[] args) {

        //define a folder root
        Path myDir = Paths.get("D:/data");  

        	loops(myDir);

       
    }
    
    public static void loops(Path myDir){
    	 try {
    		 if(!bandiera) return;
             WatchService watcher = myDir.getFileSystem().newWatchService();
             myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, 
             StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);

             WatchKey watckKey = watcher.take();

             List<WatchEvent<?>> events = watckKey.pollEvents();
             for (WatchEvent event : events) {
                  if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                      System.out.println("Created: " + event.context().toString());
                  }
                  if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
                      System.out.println("Delete: " + event.context().toString());
                  }
                  if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                      System.out.println("Modify: " + event.context().toString());
                  }
              }
             
         	loops(myDir);
             
          } catch (Exception e) {
              System.out.println("Error: " + e.toString());
          }
    }
}

By ImaginSystems & Queen Gin   

Categoria: JAVA
martedì, 23 lug 2013 Ore. 15.42

Messaggi collegati


Ora e Data
Mappa
Blogs Amici
    Copyright © 2002-2007 - Blogs 2.0
    dotNetHell.it | Home Page Blogs
    ASP.NET 2.0 Windows 2003