FileAlterationObserver observer = new FileAlterationObserver("pathToDir"); FileAlterationMonitor monitor = new FileAlterationMonitor(POLL_INTERVAL); FileAlterationListener listener = new FileAlterationListenerAdaptor() { @Override public void onFileCreate(File file) { // code for processing creation event } @Override public void onFileDelete(File file) { // code for processing deletion event } @Override public void onFileChange(File file) { // code for processing change event } }; observer.addListener(listener); monitor.addObserver(observer); monitor.start();
References
http://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library