In this article I am going to show you how to write a simple file monitor. So even though Java 7 comes with a low-level API to watch for file system changes (article here), fow now we will be using using the Commons IO library from the Apache Foundation, mainly the package.

The first step will be to define the location that we are going to monitor. For this I’ve created a temporary folder of my desktop, and defined a String constant pointing to that newly created location:

The next step will be to define a polling interval: how often we will “look” for file-system changes. The value is expressed in milliseconds:

Now we will have to build a File object out of the folder we are monitoring:

At this point Commons IO comes into picture. In order to make the system monitor actually work we will need at least one instance of the following: FileAlterationObserver, FilterAlterationMonitor and FileAlterationListenerAdaptor .

And then we will proceed to add the listener to the observer, add the observer to the monitor, and start the monitor:

After compiling & running the resulting code, every change I do in the folder that I monitor is being recorded:

The full sourcecode for the simple file monitor:

If you are not satisfied with this approach I strongly recommend you to read this tutorial about the new Java NIO.2 Watch Service api. I think this is the desirable approach if you are lucky enough to use version 7:

Java 7 NIO.2 Tutorial – Writing a simple file/folder monitor using the Watch Service API

9 thoughts on “Writing a simple file monitor in Java using Commons IO

  1. I wrote file monitor anonymous class inside function.
    All statement inside function run before anonymous class.But i want return file name after file found .

  2. Pingback: Apache File Monitor Locking Files | Developers Questions -

  3. I used the same to watch my Linux share folder. Somehow when the watched file arrives, it triggers multiple times of the onFileCreate. Any idea why?

    • Even I am using for NFS folder in linux. will the the monitor opens more files on file create? Anyone has faced a similar issue?

  4. It is not working or watching an FTP server directory, It gives the exception, actually it reads the current system path, windows path not a remote path means not seeing the ftp directory.

    Please give me some advice that I can watch the FTP directory.


Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url=""> 


Are we human, or are we dancer *