listen a directory on a FTP server
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Bill_Pattchen
Posted On:   Monday, August 28, 2006 03:09 PM

hi!


I want to write a class that works like a FTP directory listener that is, it listens for any changes in a FTP directory (particularly new files) and executes a treatment related to the file added (or deleted or modified).


Is there libraries in JAVA that accomplish this? If not,may you give me an efficient method to achieve this task?


Thanks!

Re: listen a directory on a FTP server

Posted By:   WarnerJan_Veldhuis  
Posted On:   Monday, August 28, 2006 05:09 PM

I don't know of any existing DirectoryListeners although it's not hard to create. Create a separate thread to looks for any changes. Changes might be files deleted, added, update etc. it might look something like this:

public interface DirectoryListener {
public void fileAdded( File f);
public void fileDeleted( File f);
public void fileUpdated( File f);
}


public class DirectoryListenerThread implements Runnable {

protected List listeners = new ArrayList();
protected File directory;
protected boolean hasToListen = false;

public DirectoryListenerThread( File dir) {
this.directory = dir;
}

public void run() {
hasToListen = true;
while (hasToListen) {
//scan directory here and notify the listeners when changes are made
if ( someFile has changed ) {
for ( DirectoryListener d : listeners ) {
d.fileUpdated(someFile);
}
}

if ( someFile is deleted) {
for ( DirectoryListener d : listeners ) {
d.fileDeleted(someFile);
}
}

}
}

public void addDirectoryListener( DirectoryListener d) {
listeners.add(d);
}

public void stopListening() {
this.hasToListen = false;
}


}


All you need to do now is to create a class that implements DirectoryListener, create a Thread that runs DirectoryListenerThread, add your listener to the thread and you'll get notified...



God, I love my job :)
About | Sitemap | Contact