package com.junge.spring.demo.commonio; import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; import org.apache.commons.io.monitor.FileAlterationMonitor; import org.apache.commons.io.monitor.FileAlterationObserver; import java.io.File; /** * 文件监控工具 * * @author liuxj * @create 2018-02-11 22:02 */ public class FileMonitorTest { public static void main(String[] args) throws Exception { MyFileLister myFileLister = new MyFileLister(); FileAlterationObserver observer1 = new FileAlterationObserver("I:\\test"); observer1.addListener(myFileLister); FileAlterationMonitor monitor = new FileAlterationMonitor(10000); monitor.addObserver(observer1); monitor.start(); //monitor.stop(); //停止监听 } } class MyFileLister extends FileAlterationListenerAdaptor { @Override public void onStart(FileAlterationObserver observer) { System.out.println("onStart"); } @Override public void onDirectoryCreate(File directory) { System.out.println("onDirectoryCreate"); } @Override public void onDirectoryChange(File directory) { System.out.println("onDirectoryChange"); } @Override public void onDirectoryDelete(File directory) { System.out.println("onDirectoryDelete"); } @Override public void onFileCreate(File file) { System.out.println("onFileCreate,file=" + file.getName()); } @Override public void onFileChange(File file) { System.out.println("onFileChange,file=" + file.getName()); } @Override public void onFileDelete(File file) { System.out.println("onFileDelete,file=" + file.getName()); } @Override public void onStop(FileAlterationObserver observer) { System.out.println("onStop"); } }