Monday, January 25, 2010

Java Folder Listener/Monitor

A while back, i needed to monitor a folder for various events i.e. new files added or deleted. After rummaging through the net, this is what I finally came up with.

I used a thread that kept on checking the folder new files after a certain interval. The application also detected if a file was deleted. The application then keeps track of the folder activities in a log file that I create. The log file contains the date and time of the actions performed on the selected folder.

I've pasted the code here for reference. To run the application, you need to follow the following steps:

  1. On FileApp.java line 10, change the parameters. The first parameter is the folder to monitor, while the second parameter is the location of the log file that will be created i.e. FileListenerLog.log
  2. Compile and run FileApp.java
  3. Now perform someactions on the selected folder like adding some file in the folder or deleting a file from the folder. Then make sure to check out the log file i.e. FileListenerLog.log, in the location you specified. The log file contains actions performed on the folder with the time and date of the action.
FileMonitor.java
 import java.io.BufferedWriter;  
 import java.io.File;  
 import java.io.FileWriter;  
 import java.io.IOException;  
 import java.util.ArrayList;  
 import java.util.Date;  
 import java.util.List;  
 public class FileMonitor implements Runnable {  
      static int fileCount;  
      static Thread t;  
      static File[] existingFiles;  
      static int counterCheck = 0;  
      List list;  
      String folderToMonitor; // Directory to monitor  
      String logFileToCreate;  
      FileMonitor(String folderToMonitor, String logFileToCreate) {  
           this.folderToMonitor = folderToMonitor;  
           this.logFileToCreate = logFileToCreate;  
      }  
      public void run() {  
           t = FileApp.t;  
           Date date = new Date();  
           int currentFilecount = 0;  
           File f = new File(this.folderToMonitor);  
           File log = new File(this.logFileToCreate + "/FileListenerLog.log");  
           BufferedWriter bufferedWriter = null;  
           FileWriter fstream = null;  
           boolean status = f.isDirectory();  
           File[] currentFiles = (File[]) null;  
           if (status) {  
                currentFilecount = f.listFiles().length;  
                if (counterCheck == 0) {  
                     existingFiles = f.listFiles();  
                     fileCount = currentFilecount;  
                     counterCheck += 1;  
                }  
           } else {  
                f.mkdir();  
           }  
           if (fileCount != currentFilecount) {  
                if (fileCount > currentFilecount) {  
                     String str = date.toString() + ": Files have been Deleted :"  
                               + (fileCount - currentFilecount);  
                     try {  
                          fstream = new FileWriter(log, true);  
                          bufferedWriter = new BufferedWriter(fstream);  
                          currentFiles = f.listFiles();  
                          bufferedWriter.newLine();  
                          bufferedWriter.write(str);  
                          bufferedWriter.flush();  
                          this.list = compareArray(existingFiles, currentFiles, bufferedWriter);  
                          fileCount = currentFilecount;  
                          existingFiles = currentFiles;  
                          bufferedWriter.close();  
                     } catch (IOException e) {  
                          e.printStackTrace();  
                     }  
                } else {  
                     String str1 = date.toString() + ": New files have been Added: "  
                               + (currentFilecount - fileCount);  
                     try {  
                          fstream = new FileWriter(log, true);  
                          bufferedWriter = new BufferedWriter(fstream);  
                          currentFiles = f.listFiles();  
                          bufferedWriter.newLine();  
                          bufferedWriter.write(str1);  
                          bufferedWriter.flush();  
                          this.list = compaireArray(existingFiles, currentFiles, bufferedWriter);  
                          fileCount = currentFilecount;  
                          existingFiles = currentFiles;  
                          bufferedWriter.close();  
                     } catch (IOException e) {  
                          e.printStackTrace();  
                     }  
                }  
           }  
           try {  
                Thread.sleep(7000L);  
           } catch (InterruptedException e) {  
                e.printStackTrace();  
           }  
           t.run();  
      }  
      private List compareArray(File[] existingFiles2, File[] currentFiles,  
                BufferedWriter buffWriter) {  
           List list = new ArrayList();  
           int counter = 0;  
           boolean flag = true;  
           for (int i = 0; i < existingFiles2.length; ++i) {  
                for (int j = 0; j < currentFiles.length; ++j) {  
                     if (existingFiles2[i].getName().equals(  
                               currentFiles[j].getName())) {  
                          flag = true;  
                          break;  
                     }  
                     flag = false;  
                }  
                if (!(flag)) {  
                     list.add(existingFiles2[i]);  
                     ++counter;  
                }  
           }  
           if (list != null) {  
                for (int i = 0; i < list.size(); ++i) {  
                     String str = null;  
                     if (((File) list.get(i)).isDirectory()) {  
                          str = "Folder:" + ((File) list.get(i)).getName();  
                     } else {  
                          System.out  
                                    .println("File:" + ((File) list.get(i)).getName());  
                          str = "File:" + ((File) list.get(i)).getName();  
                     }  
                     try {  
                          buffWriter.newLine();  
                          buffWriter.write(str);  
                          buffWriter.flush();  
                     } catch (IOException e) {  
                          e.printStackTrace();  
                     }  
                }  
           }  
           return list;  
      }  
      private List compaireArray(File[] existingFiles2, File[] currentFiles,  
                BufferedWriter buffWriter) {  
           List list = new ArrayList();  
           int counter = 0;  
           boolean flag = true;  
           for (int i = 0; i < currentFiles.length; ++i) {  
                for (int j = 0; j < existingFiles2.length; ++j) {  
                     if (currentFiles[i].getName().equals(  
                               existingFiles2[j].getName())) {  
                          flag = true;  
                          break;  
                     }  
                     flag = false;  
                }  
                if (!(flag)) {  
                     list.add(currentFiles[i]);  
                     ++counter;  
                }  
           }  
           if (list != null) {  
                for (int i = 0; i < list.size(); ++i) {  
                     String str = null;  
                     if (((File) list.get(i)).isDirectory()) {  
                          str = "New Folder:" + ((File) list.get(i)).getName();  
                     } else  
                          str = "New File:" + ((File) list.get(i)).getName();  
                     try {  
                          buffWriter.newLine();  
                          buffWriter.write(str);  
                          buffWriter.flush();  
                     } catch (IOException e) {  
                          e.printStackTrace();  
                     }  
                }  
           }  
           return list;  
      }  
      public List getNewFile() {  
           return this.list;  
      }  
 }  


FileApp.java
 public class FileApp {  
      static Thread t;  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           FileMonitor monitor = new FileMonitor("/home/frankline/Desktop/xml", "/home/frankline");  
           t = new Thread(monitor);  
           t.start();  
      }  
 }  

Hope this helps.

Monday, January 11, 2010

Ubuntu - Bottom panel not showing open applications

I gave my younger brother my laptop to use. Later I noticed that none of my open windows were showing on the bottom panel taskbar.

I'm guessing he deleted the applet that shows the open applications.

To resolve, I added the applet again by doing the following:

  1. Right click on the bottom panel
  2. Select 'Add to Panel'
  3. Click on 'Window List'
  4. Click on the 'Add' button.
Hope this helps.