Skip to content

Instantly share code, notes, and snippets.

@jyeary
Last active August 7, 2017 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyeary/d77706d5e96f7c3f3385cb650de3d14d to your computer and use it in GitHub Desktop.
Save jyeary/d77706d5e96f7c3f3385cb650de3d14d to your computer and use it in GitHub Desktop.
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());
}
}
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) {
}
}
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment