file / directory listener

Discussion in 'Java' started by James, Jul 5, 2004.

  1. James

    James Guest

    I'd like to write a file / directory listener that would listen for
    modifications, creations, and name changes of files and directories.
    What's the best way to go about this? Is there a project that have done
    this already?
     
    James, Jul 5, 2004
    #1
    1. Advertising

  2. James

    Roedy Green Guest

    On Mon, 05 Jul 2004 19:22:31 GMT, James <> wrote
    or quoted :

    >I'd like to write a file / directory listener that would listen for
    >modifications, creations, and name changes of files and directories.
    >What's the best way to go about this? Is there a project that have done
    >this already?


    This will require a hook into the native OS to receive change
    notifications. That is by far the trickiest part of the problem.

    see http://mindprod.com/projects/filefinder.html
    http://mindprod.com/projects/javafilesystem.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 5, 2004
    #2
    1. Advertising

  3. James

    Jacob Guest

    James wrote:

    > I'd like to write a file / directory listener that would listen for
    > modifications, creations, and name changes of files and directories.
    > What's the best way to go about this? Is there a project that have done
    > this already?


    The simplest way to acheive this is by polling.
    I have an implementation of this which you may
    use (attached). The listener class must implement
    the FileListener interface:

    public interface FileListener
    {
    void fileChanged (File file);
    }


    The listener class must instantiate a FileMonitor
    object, identify which files/directories to listen
    for, and add itself as a listener:

    // Poll once every second (1000ms)
    FileMonitor fileMonitor = new FileMonitor (1000);

    fileMonitor.addFile (new File ("/some/file"));
    fileMonitor.addFile (new File ("/some/directory"));
    fileMonitor.addFile (new File ("/one/more/file"));

    fileMonitor.addListener (this);


    Complete listing of the FileMonitor class:



    import java.util.*;
    import java.io.File;
    import java.lang.ref.WeakReference;


    /**
    * Class for monitoring changes in disk files.
    * Usage:
    *
    * 1. Implement the FileListener interface.
    * 2. Create a FileMonitor instance.
    * 3. Add the file(s)/directory(ies) to listen for.
    *
    * fileChanged() will be called when a monitored file is created,
    * deleted or its modified time changes.
    */
    public class FileMonitor
    {
    private Timer timer_;
    private HashMap files_; // File -> Long
    private Collection listeners_; // of WeakReference(FileListener)


    public FileMonitor (long pollingInterval)
    {
    files_ = new HashMap();
    listeners_ = new ArrayList();

    timer_ = new Timer (true);
    timer_.schedule (new FileMonitorNotifier(), 0, pollingInterval);
    }


    public void stop()
    {
    timer_.cancel();
    }


    public void addFile (File file)
    {
    if (!files_.containsKey (file)) {
    long modifiedTime = file.exists() ? file.lastModified() : -1;
    files_.put (file, new Long (modifiedTime));
    }
    }


    public void removeFile (File file)
    {
    files_.remove (file);
    }


    public void addListener (FileListener fileListener)
    {
    // Don't add if its already there
    for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
    WeakReference reference = (WeakReference) i.next();
    FileListener listener = (FileListener) reference.get();
    if (listener == fileListener)
    return;
    }

    // Use WeakReference to avoid memory leak if this becomes the
    // sole reference to the object.
    listeners_.add (new WeakReference (fileListener));
    }


    public void removeListener (FileListener fileListener)
    {
    for (Iterator i = listeners_.iterator(); i.hasNext(); ) {
    WeakReference reference = (WeakReference) i.next();
    FileListener listener = (FileListener) reference.get();
    if (listener == fileListener) {
    i.remove();
    break;
    }
    }
    }


    private class FileMonitorNotifier extends TimerTask
    {
    public void run()
    {
    // Loop over the registered files and see which have changed.
    // Use a copy of the list in case listener wants to alter the
    // list within its fileChanged method.
    Collection files = new ArrayList (files_.keySet());

    for (Iterator i = files.iterator(); i.hasNext(); ) {
    File file = (File) i.next();
    long lastModifiedTime = ((Long) files_.get (file)).longValue();
    long newModifiedTime = file.exists() ? file.lastModified() : -1;

    // Chek if file has changed
    if (newModifiedTime != lastModifiedTime) {

    // Register new modified time
    files_.put (file, new Long (newModifiedTime));

    // Notify listeners
    for (Iterator j = listeners_.iterator(); j.hasNext(); ) {
    WeakReference reference = (WeakReference) j.next();
    FileListener listener = (FileListener) reference.get();

    // Remove from list if the back-end object has been GC'd
    if (listener == null)
    j.remove();
    else
    listener.fileChanged (file);
    }
    }
    }
    }
    }
    }
     
    Jacob, Jul 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danish Ahmed

    .net remoting and tcp listener socket

    Danish Ahmed, Apr 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,787
    Danish Ahmed
    Apr 21, 2005
  2. not entered

    Socket Listener not available

    not entered, Mar 31, 2005, in forum: Perl
    Replies:
    1
    Views:
    2,720
    Joe Smith
    Mar 31, 2005
  3. =?Utf-8?B?c3R3QGlkc2k=?=

    Frame Listener

    =?Utf-8?B?c3R3QGlkc2k=?=, May 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    543
    =?Utf-8?B?c3R3QGlkc2k=?=
    May 2, 2004
  4. Jagan
    Replies:
    0
    Views:
    435
    Jagan
    Aug 8, 2003
  5. oneperson

    Looping (Directory Listener?)

    oneperson, Aug 18, 2007, in forum: Java
    Replies:
    0
    Views:
    569
    oneperson
    Aug 18, 2007
Loading...

Share This Page