How to detect file changes using JAVA (FileChangeEvent)


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

In this post, I will demonstrate how to detect file change events by using Apache Commons VFS API.

First download required libraries from the following links;
commons-vfs-1.0.jar
commons-logging-1.1.1.jar

And add these libraries in your project’s lib folder and set a reference for the build path. After this, create a config.properties
file under your project’s source folder for monitoring. (File name could be everything such as abc.txt …)

And create a FileMonitoring.java class like the following one;

After running program, if the user changes the file (modification like changing content) then the output will be similar;

package filelistenerexample;

import java.io.File;
import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
*
* @author tunatore
*/
public class FileMonitoring implements FileListener {

private static int counter = 0; //for versioning
private static DefaultFileMonitor fileMonitor = null;
private static File configFile = new File(FileMonitoring.class
.getResource("config.properties").getFile().replace("%20", " "));
//file for monitoring changes

public static void main(String[] args) throws FileSystemException, InterruptedException {
FileMonitoring fileMonitoring = new FileMonitoring();
fileMonitoring.startListening(fileMonitoring);
//start listening file changes
}

public void startListening(FileMonitoring fileMonitoring) throws FileSystemException, InterruptedException {
FileSystemManager fsManager = VFS.getManager();
FileObject fileObject = fsManager.toFileObject(configFile);
if (fileMonitor == null) {
fileMonitor = new DefaultFileMonitor(this);
fileMonitor.addFile(fileObject);
fileMonitor.start();
}
synchronized (fileMonitoring) {
fileMonitoring.wait(); //wait fileChange
startListening(fileMonitoring); //recursive call
}

}

public void fileCreated(FileChangeEvent fce) throws Exception {
throw new NotImplementedException();
}

public void fileDeleted(FileChangeEvent fce) throws Exception {
throw new NotImplementedException();
}

public void fileChanged(FileChangeEvent fce) throws Exception {
System.out.println("fileChanged:version-->" + ++counter);
synchronized (this) { //notify waiting thread
this.notify();
}
}
}

run:
13.May.2011 23:38:42 org.apache.commons.vfs.VfsLog info
INFO: Using “C:\DOCUME~1\tunat\LOCALS~1\Temp\vfs_cache” as temporary files store.
fileChanged:version–>1
fileChanged:version–>2
fileChanged:version–>3
fileChanged:version–>4
fileChanged:version–>5
fileChanged:version–>6
fileChanged:version–>7
Java Result: 2147483647
BUILD SUCCESSFUL (total time: 22 seconds)

Download Source

Advertisements

5 thoughts on “How to detect file changes using JAVA (FileChangeEvent)

  1. Can you give me more of a clue? 🙂

    I’d like my application which utilises DefaultFileMonitor to run as a daemon but I’d like a user to be able to stop it gracefully.

  2. Can you explain whats the difference between Apache VFS and Apache commons-io file monitoring service?
    And which one is better?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s