Java – Monitor Directory for File Creation

In batch processing, there are use-cases that require event triggers to start operations on data sources, e.g., files or remote web endpoint. These could be user-initiated events like clicking a button. They could even be system-generated ones like file creation. This post demonstrates how to monitor a local file system directory for new files for processing using only Java.


We could use JPoller for this. However, Java (since 1.7) has these facilities to monitor object changes and events. For interest in JPoller, please read Using JPoller.

News About

JPoller was originally hosted in http://jpoller.sourceforge.net/. I believe the author has moved it to GitHub. However, there are no new major changes to the source codes.

Try to Extend JPoller

Some have tried to extend it but found it difficult to do so. I personally find it difficult to extend.

Java WatchService

WatchService in an interface from the java.nio package. See https://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html. The way to get an instance of it is via FileSystems.getDefault()  or FileSystems.getFileSystem(URI) .

A Directory to Monitor

With an instance of WatchService , we can now register it with a Path  directory.

Hereafter, we need to poll the directory.

Purchase POC

The complete POC codes are available for USD 5. Please visit https://www.facebook.com/turreta/

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert