WatchService API allows you to monitor a directory for changes, such as file creation, deletion, or modification, without polling.
Source Code
try (WatchService service = FileSystems.getDefault().newWatchService()) {
Path path = Paths.get(System.getProperty("user.home"));
path.register(service, StandardWatchEventKinds.ENTRY_CREATE);
WatchKey key;
while ((key = service.take()) != null) {
for (WatchEvent event : key.pollEvents()) {
System.out.println("Event kind:" + event.kind()
+ ". File affected: " + event.context() + ".");
}
key.reset();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}