I set up an example based on the Watchingservice API.
The comments in the code explain what each command does and should give a good idea of how to process the files within a scenario similar to the one described in the question.
The code:
//define pasta a ser observada
Path pastaOrigem = Files.createDirectories(Paths.get("origem"));
//define pasta onde os arquivos sendo processados serão movidos
Path pastaProcessando = Files.createDirectories(pastaOrigem.resolve("processando"));
//define pasta onde os arquivos já processados serão movidos
Path pastaProcessados = Files.createDirectories(pastaOrigem.resolve("processados"));
//monitor da pasta
WatchService watcher = FileSystems.getDefault().newWatchService();
//registra para receber eventos de criação de arquivos no diretório
pastaOrigem.register(watcher, ENTRY_CREATE);
//loop infinito
while (true) {
WatchKey wk = null;
try {
//aguarda algum evento ocorrer
System.out.printf("Aguardando arquivos em %s", pastaOrigem);
wk = watcher.take();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
//percorre eventos recebidos
for (WatchEvent<?> event : wk.pollEvents()) {
//ignora se ocorreu um problema
if (event.kind() == OVERFLOW) continue;
//pega nome do arquivo criado
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path nomeArquivo = ev.context();
Path arquivoOrigem = pastaOrigem.resolve(nomeArquivo);
System.out.printf("Arquivo criado: %s\n", arquivoOrigem);
//move arquivo para "processando"
Path arquivoProcessando = pastaProcessando.resolve(nomeArquivo);
System.out.printf("Movendo para: %s\n", arquivoProcessando);
Files.move(arquivoOrigem, arquivoProcessando);
//processa arquivo
System.out.printf("Processando arquivo %s\n", arquivoProcessando);
//move para "processados"
Path arquivoProcessado = pastaProcessados.resolve(nomeArquivo);
System.out.printf("Movendo para: %s\n", arquivoProcessado);
Files.move(arquivoProcessando, arquivoProcessado);
}
//prepara o próximo evento ou sai caso algum erro ocorra, como o diretório não existir mais
if (!wk.reset()) {
break;
}
}
//encerramento manual, caso neccesário
//watcher.close();
There are two things to consider not directly related to this API:
In the above example, the files created in the monitored directory are moved later to a directory processando
and processados
. This is done to decrease problems where the same file can be read more than once, for example. Also, if an error occurs while processing the file, it can be moved to a folder erro
so that it can be analyzed what happened. This is just a hint of how to handle the file read stream and avoid leaving it all in one folder, which can cause many problems.
One point to consider is do not generate files directly in the observed directory. If the system that generates the file does this, the file can be read at the time of creation, before of the system complete the writing until the end. Then the ideal is to put a file in a temporary directory and then move the file to the monitored directory after its generation or transfer is fully completed. Preferably, have some mechanism to validate whether the read file is consistent and avoid surprises (such as half imports).
There are two possible situations that you haven’t made clear which one you want to handle: you just want to check if the file exists and perform an action with it or you want your application to monitor a certain folder until the file is created?
– user28595