1 minute read

Even though Java 7 comes with a low-level API to watch for file system changes (article here), there’s also the option to use the Commons IO library from the Apache Foundation, mainly the org.apache.commons.io.monitor package.

The first step will be to define the location that we are going to monitor:

public static final String FOLDER =
        "/home/skywalker/Desktop/simple-test-monitor/watchdir";

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:

final long pollingInterval = 5 * 1000;

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

File folder = new File(FOLDER);

At this point, Commons IO comes into the picture.

In order to make the system monitor actually work we will need at least one instance of the following: 

FileAlterationObserver observer = new FileAlterationObserver(folder);
FileAlterationMonitor monitor = new FileAlterationMonitor(pollingInterval);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
    // Is triggered when a file is created in the monitored folder
    @Override
    public void onFileCreate(File file) {
        try {
            // "file" is the reference to the newly created file
            System.out.println("File created: "
                    + file.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }

    // Is triggered when a file is deleted from the monitored folder
    @Override
    public void onFileDelete(File file) {
        try {
            // "file" is the reference to the removed file
            System.out.println("File removed: "
                    + file.getCanonicalPath());
            // "file" does not exists anymore in the location
            System.out.println("File still exists in location: "
                    + file.exists());
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
}

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

observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();

After compiling & running the resulting code, every change will be recorded:

File created: /home/skywalker/Desktop/simple-test-monitor/watchdir/1
File created: /home/skywalker/Desktop/simple-test-monitor/watchdir/test
File created: /home/skywalker/Desktop/simple-test-monitor/watchdir/test2
File removed: /home/skywalker/Desktop/simple-test-monitor/watchdir/test
File still exists in location: false

Updated:

Comments