How can I stop the execution:
key = watchService.take()
my code:
//Watcher
public class DirectoryWatcherExample {
public static void main(String[] args) throws IOException, InterruptedException {
WatchService watchService = FileSystems.getDefault().newWatchService();
//My folder
Path path = Paths.get("D:\\java\\Input");
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
WatchKey key;
while (((key = watchService.take()) != null)) {
System.out.println(key);
System.out.println(key.toString());
for (WatchEvent<?> event : key.pollEvents()) {
if(event.context().toString().contains(".dat")){
System.out.println("FileName: "+event.context());
}
}
key.reset();
}
watchService.close();
}
}
my code is waiting for the execution of that line, is it possible to somehow stop the execution, tried:
key.cancel();
watchService.close()
but this did not give any result, can you tell me how to solve this problem?