Crear un oyente en Java [duplicado]

Oct 01 2020

Quiero crear un programa en Java / Spring que escuche siempre un directorio. Si se crea un nombre en particular, por ejemplo, "example.txt" en este directorio, el programa lo lee, hace algunas cosas y luego continúa escuchando. Si este archivo txt no se crea, no pasa nada y continúa escuchando.

¿Alguna idea de cómo puedo crear esta parte de escucha?

Muchas gracias por tu ayuda

Respuestas

1 Guillaume Oct 01 2020 at 17:13

Puede usar WatchService agregado en Java 7

WatchService watchService = FileSystems.getDefault().newWatchService();
Path path = Paths.get("example.txt");
WatchKey watchKey = path.register(watchService, StandardWatchEventKinds...);

Más información aquí y aquí