Monitor files for changes : File Monoitor « File Input Output « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » File Input Output » File MonoitorScreenshots 
Monitor files for changes
 
// Copyright (C) 2007 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


import java.io.File;
import java.util.Hashtable;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Monitor files for changes. This singleton class maintains a map of files to
 * monitor and objects to notify when something they change.
 */
public class FileMonitor {

  private static final FileMonitor SINGLETON = new FileMonitor();

  private Timer timer;
  private Hashtable<String, TimerTask> timerTasks;

  private FileMonitor() {
    timer = new Timer(true);
    timerTasks = new Hashtable<String, TimerTask>();
  }

  /**
   * Returns the singleton instance of this class.
   @return the singleton instance
   */
  public static FileMonitor getInstance() {
    return SINGLETON;
  }

  /**
   * Start monitoring a file.
   
   @param listener listener to notify when the file changed.
   @param fileName name of the file to monitor.
   @param period polling period in milliseconds.
   */
  public void addFileChangeListener(FileChangeListener listener,
      String fileName, long period) {
    removeFileChangeListener(listener, fileName);
    FileMonitorTask task = new FileMonitorTask(listener, fileName);
    timerTasks.put(fileName + listener.hashCode(), task);
    timer.schedule(task, period, period);
  }

  /**
   * Remove the listener from the notification list.
   
   @param listener the listener to be removed.
   */
  public void removeFileChangeListener(FileChangeListener listener,
      String fileName) {
    FileMonitorTask task = (FileMonitorTasktimerTasks.remove(fileName
        + listener.hashCode());
    if (task != null) {
      task.cancel();
    }
  }

  protected void fireFileChangeEvent(FileChangeListener listener,
      String fileName) {
    listener.fileChanged(fileName);
  }

  class FileMonitorTask extends TimerTask {
    FileChangeListener listener;
    String fileName;
    File monitoredFile;
    long lastModified;

    public FileMonitorTask(FileChangeListener listener, String fileName) {
      this.listener = listener;
      this.fileName = fileName;
      this.lastModified = 0;
      monitoredFile = new File(fileName);
      this.lastModified = getLastModified();
    }

    private long getLastModified() {
      if (monitoredFile.exists()) { 
        return monitoredFile.lastModified();
      else {
        return -1;
      }
    }

    @Override
    public void run() {
      long lastModified = getLastModified();
      if (lastModified != this.lastModified) {
        this.lastModified = lastModified;
        fireFileChangeEvent(this.listener, this.fileName);
      }
    }
  }
  
  public interface FileChangeListener {
    public void fileChanged(String fileName);
  }

}

   
  
Related examples in the same category
1.File Monitor Demo
2.Utility class for synchronizing files/directories
3.Monitoring a File for changes.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.