This example uses a
FileAlterationListener
to monitor file changes. It is simple to implement.
The code for the project can be found here: file-monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bluelotussoftware.commons.io; | |
import java.io.File; | |
import java.util.Date; | |
import org.apache.commons.io.monitor.FileAlterationListener; | |
import org.apache.commons.io.monitor.FileAlterationObserver; | |
/** | |
* | |
* @author John Yeary | |
* @version 1.0 | |
*/ | |
public class FileAlterationListenerImpl implements FileAlterationListener { | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onStart(final FileAlterationObserver observer) { | |
System.out.println("The WindowsFileListener has started on " + observer.getDirectory().getAbsolutePath()); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onDirectoryCreate(final File directory) { | |
System.out.println(directory.getAbsolutePath() + " was created."); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onDirectoryChange(final File directory) { | |
System.out.println(directory.getAbsolutePath() + " wa modified"); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onDirectoryDelete(final File directory) { | |
System.out.println(directory.getAbsolutePath() + " was deleted."); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onFileCreate(final File file) { | |
System.out.println(file.getAbsoluteFile() + " was created."); | |
System.out.println("----------> length: " + file.length()); | |
System.out.println("----------> last modified: " + new Date(file.lastModified())); | |
System.out.println("----------> readable: " + file.canRead()); | |
System.out.println("----------> writable: " + file.canWrite()); | |
System.out.println("----------> executable: " + file.canExecute()); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onFileChange(final File file) { | |
System.out.println(file.getAbsoluteFile() + " was modified."); | |
System.out.println("----------> length: " + file.length()); | |
System.out.println("----------> last modified: " + new Date(file.lastModified())); | |
System.out.println("----------> readable: " + file.canRead()); | |
System.out.println("----------> writable: " + file.canWrite()); | |
System.out.println("----------> executable: " + file.canExecute()); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onFileDelete(final File file) { | |
System.out.println(file.getAbsoluteFile() + " was deleted."); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void onStop(final FileAlterationObserver observer) { | |
System.out.println("The WindowsFileListener has stopped on " + observer.getDirectory().getAbsolutePath()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bluelotussoftware.commons.io; | |
import java.io.File; | |
import org.apache.commons.io.monitor.FileAlterationMonitor; | |
import org.apache.commons.io.monitor.FileAlterationObserver; | |
/** | |
* | |
* @author John Yeary | |
* @version 1.0 | |
*/ | |
public class FileMonitor { | |
public static void main(String[] args) throws Exception { | |
// Change this to match the environment you want to watch. | |
final File directory = new File("/Users/jyeary/Desktop"); | |
FileAlterationObserver fao = new FileAlterationObserver(directory); | |
fao.addListener(new FileAlterationListenerImpl()); | |
final FileAlterationMonitor monitor = new FileAlterationMonitor(); | |
monitor.addObserver(fao); | |
System.out.println("Starting monitor. CTRL+C to stop."); | |
monitor.start(); | |
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
System.out.println("Stopping monitor."); | |
monitor.stop(); | |
} catch (Exception ignored) { | |
} | |
} | |
})); | |
} | |
} |
0 comments :
Post a Comment