Monitoring a Filesystem – Part 2

So, in my previous post  of this series, I shared my experience where we were continuously polling the filesystem for any updates. We have also seen what were the disadvantages of such systems. We started exploring other tools, that were similar in nature and were thus, discarded. One such option that we tried was the Apache commons IO library package.

We will create a class and implement the FileAlterationListener interface. We will override the callback methods, onStart, onStop, onFileChange, onDirectoryCreate etc. So now, we are listening to these callbacks for certain events to be triggered.

File dirToWatch = new File("/appl/media/misc");
FileAlterationObserver observer = new FileAlterationObserver(dirToWatch); 
observer.addListener(new FileWatcherListenerUsingApache());

final FileAlterationMonitor monitor = new FileAlterationMonitor();
monitor.addObserver(observer);
monitor.start();

Now, say, we have a directory to watch. So, we create a File object with the directory location. Then, we initialize a FileAlterationObserver object with the File object, and then register the Listener to the observer object. We then create a FileAlterationMonitor object and register the observer object with it. Then we invoke the start method on the monitor.

This library is very easy to implement and can be used when the data set is finite and small. You can get a sample implementation on github. Feel free to download and tinker around a bit.

Coming back to the problem at hand, while all of that is fine, this guy keeps on scanning on a regular basis for any changes in the filesystem. So, as the data size increase, it becomes a bottle neck in the efficiency. Also, there is a massive impact on the system performance.

A very light-weight, efficient and fast option is to use Java’s nio package that I am going to cover in the next post, that also happens to be the concluding post of this series.

Reference:

Published by Sam Banerjee

I’m an AI and software engineering consultant who helps organizations design and deliver production-ready AI systems. I specialize in translating complex machine learning ideas into scalable, reliable solutions that work under real-world constraints. My focus spans AI architecture, applied ML, and system design, with an emphasis on model behavior, generalization, and operational risk. I work end-to-end—from problem framing and technical strategy to execution and deployment—bringing clarity, rigor, and pragmatism to AI initiatives.

Leave a comment