Java, Software Development

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 endpoints. 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 at 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 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.

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *