I have an application that monitors a certain folder and checks if there is a new file creation if a new file is created by triggering multiple processes on the system.
But when I activate the monitoring it seems it locks up multiple processes and allows me almost no interaction with my system.
public void Monitoramento(){
Path pastaOrigem;
try {
pastaOrigem = Files.createDirectories(Paths.get("C:\monitoramento"));
watcher = FileSystems.getDefault().newWatchService();
pastaOrigem.register(watcher, ENTRY_CREATE);
while (true) {
WatchKey wk = null;
try {
wk = watcher.take();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
for (WatchEvent<?> event : wk.pollEvents()) {
if (event.kind() == OVERFLOW) {
continue;
}
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path nomeArquivo = ev.context();
Path arquivoOrigem = pastaOrigem.resolve(nomeArquivo);
String arquivo = nomeArquivo.toString();
if (arquivo.substring(0, 3).equals("vn")) {
novoEvento ne = new novoEvento();
ne.leituraArquivo(arquivoOrigem);
}
}
if (!wk.reset()) {
break;
}
}
} catch (IOException ex) {
Logger.getLogger(TelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex){
ex.printStackTrace();
}
}