Java, Software Development

Monitor Directory for File Creation Using Only Java

In batch processing, some use-cases require event triggers to start operations on data sources, e.g., files or remote web endpoints. These could be user events like clicking a button. On the other hand, they could be system events like file creation. This post demonstrates how to monitor a local file system directory for new files for processing using only Java.

Monitor A Directory With JPoller

We could use JPoller for this. However, Java has these facilities to monitor object changes and events since 1.7. For any interest in JPoller, please read Using JPoller. But this tool is pretty old!

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

Some have tried to extend it but found difficulty in doing so. First, it is still using Apache Ant, and the libraries it uses are not available in the Git repository. Second, its dependency on other libraries is haphazard. Lastly, it lacks documentation. I personally find it difficult to extend.

Therefore, it’s safer to use Java’s WatchService without relying on third-party libraries. Of course, there are third-party libraries that we can use if we are developing an Enterprise application. These include Camel and Spring Integration.

Monitor Directory Or File With 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) .

For example, consider the following line of codes.

Example: Monitor A Directory

Now, let’s code an example. With an instance of WatchService, we can now register it with a Path directory.

The codes will monitor the C:/monitored directory for file creation and we can do that in an infinite while loop.

And that’s how to monitor a local file or directory without using third-party libraries in Java.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like