Sunday, August 23, 2009

Listen to file changes

Ever wanted your program to register changes happening to a file without writing much code? Apache VFS is here to help you with that!

public class SampleFileListener {

 
 public static void main(String[] args) {
  try {
   
   // FileSystemManager manages the file system
   FileSystemManager fsm = VFS.getManager();
   // FileSystemManager instance is used to get a FileObject instance
   // for the file to which you wish to listen. Here, the changes on 
   // the file temp.txt directly under D: would be registered.
   FileObject fileObject = fsm.resolveFile("D:\\temp.txt");
   
   // DefaultFileMonitor is the object which would monitor the file. 
   // The javadoc says that the class is a Thread based polling system 
   // monitor with 1 second delay. This means when the start method on
   // an instance of DefaultFileMonitor is invoked, a new Thread is 
   // started in the background which checks the file for changes after
   // every second. An instance of an anonymous inner class extending the
   // FileListener is passed to the constructor of DefaultFileMonitor. Its
   // the methods of this class that gets triggered every time a change
   // is detected by the file monitor.
   DefaultFileMonitor dfm = new DefaultFileMonitor(new FileListener() {
    
    // Method invoked when the file is changed and saved.
    public void fileChanged(FileChangeEvent arg0) throws Exception {
     System.out.println("File changed");
    }

    public void fileCreated(FileChangeEvent arg0) throws Exception {
     System.out.println("File created");
    }

    public void fileDeleted(FileChangeEvent arg0) throws Exception {
     System.out.println("File deleted");
    }
    
   });
   
   // Add the file object to the monitor and start the process of listening
   // for changes.
   dfm.addFile(fileObject);
   dfm.start();
   
   // Setup the main thread to run infinitely for live monitoring of changes 
   // on the file.
   while(true) {}
   
  } catch (FileSystemException e) {
   e.printStackTrace();
  }
 }
}


The above program is all that you need for registering changes on a file in your file system. No need for writing your own custom thread or file monitors. For the above class to compile and run successfully, you would need to add the VFS jar to the CLASSPATH. Download it from here. You would also need to add commons-logging.jar (version 1.0.4 or later) to the CLASSPATH.

Friday, August 21, 2009

BackItUp!

This is a simple utility which people working on a lot of data everyday may find quite useful. Once when I had just started coding professionally, I had the misfortune of losing some code due to a hard drive crash. Since then, me and my team members started following the routine of backing up the days' code to the shared drive (network attached storage) as committing un-reviewed stuff to version control was not desirable. Doing this thing manually everyday was something I disliked. Besides, people (including me [:)]) used to forget it on most days. So, I thought of automating this with a simple Java project.

The BackItUp project which has a single Java file DataBacker.java can be downloaded here.

The program accepts two command line input arguments, namely the directory/ file to be backed up and the path of the zip file (including its name) to which the contents need to be saved to. It uses classes from the Java I/O package for the file operations. After downloading and unzipping the project, open the run-program.bat and modify the paths succeeding the class name to point to ones you require. The first path points to the directory/ file to be backed up and the second is that of the zip file ending with its name. If the file path contains spaces, remember to surround it with quotes as Java reads a space as an argument separator. A pre-requisite for the program to run successfully is that JDK or JRE's bin should be added to the Windows PATH variable.

To automate the process of running the program when required, create a Windows scheduled task and associate it with the run-program.bat file. Creation of scheduled task is described in detail here.