Java - WatchService recursivo

 
Vista:

WatchService recursivo

Publicado por Juan Claudio (1 intervención) el 26/04/2012 10:08:49
Hola a todos.

Me gustaria saber, si alguien sabe hacer que el WatchServirce pueda ser recursivo y así poder tener observado todo el directorio de la ruta que pasamos como parametro al metodo.

He intentado creando una clase con el metodo en cuestion y desde otra, recorro el directorio, creo una nueva instancia de la clase y le paso el path.


public void listarDirectorio(File d){
File[] f=d.listFiles();
String[] s= new String[10];
Path p=Paths.get(d.getPath());
miObserver= new ObservarDirectorios(p,s);// cuando llega aquí se para esperando eventos
for(int i=0; i<f.length; i++){
if(f[i].isDirectory()){
s[i]=f[i].getPath();
Path p2=Paths.get(f[i].getPath());
miObserver= new ObservarDirectorios(p2,s);
imprimirdirectorio(f[i]);
}
}
}

Metodo de la clase a la que llamo.

public void seguimientoRuta(Path p, String[] args)throws IOException, InterruptedException{
Path ruta=p;
try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {
final Map<WatchKey, Path> keyMap = new HashMap<WatchKey, Path>();
for (final String arg : args.length > 0 ? args : new String[] {"."}) {
final Path path =p;
keyMap.put(path.register(watchService, ENTRY_CREATE, ENTRY_DELETE,ENTRY_MODIFY), path);
}
WatchKey watchKey;
do {
watchKey = watchService.take();
final Path eventDir = keyMap.get(watchKey);
for (final WatchEvent<?> event : watchKey.pollEvents()) {
final WatchEvent.Kind kind = event.kind();
final Path eventPath = (Path) event.context();
System.out.println(eventDir + ": " + event.kind() + ": " + event.context());
}
} while (watchKey.reset());
}

Muchas Gracias a todos y un Saludo.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder