有三种方式:1、java common.io 内部实现是遍历的方式,小文件夹的效率还好,比如我测试60G的目录,就很慢很慢了。2、jdk 7 的watch service //经测试基本不可用。在一个40g的很深的目录下去新建和删除文件5分钟都没结果。主要原因是需要对每一个Path进行注册监控。
3、jnotify 直接调用windows的api,效率很高,也很简单,推荐使用。------------------------------------------------------------------------------------------------------------------------------------------------------------common.io
需要java.io2.1及其以上版本。版本地址如下:
http://commons.apache.org/io/download_io.cgi
importorg.apache.commons.io.monitor.FileAlterationListenerAdaptor;/*** 自定义文件监听器
*@author* @date 2010-11-16
* @file org.demo.file.MyFileListener.java*/
public class MyFileListener extendsFileAlterationListenerAdaptor{
@Overridepublic voidonFileCreate(File file) {
System.out.println("[新建]:" +file.getAbsolutePath());
}
@Overridepublic voidonFileChange(File file) {
System.out.println("[修改]:" +file.getAbsolutePath());
}
@Overridepublic voidonFileDelete(File file) {
System.out.println("[删除]:" +file.getAbsolutePath());
}
}importorg.apache.commons.io.monitor.FileAlterationMonitor;importorg.apache.commons.io.monitor.FileAlterationObserver;public classTest {/***@paramargs*/
public static void main(String[] args) throwsException{//监控目录
String rootDir = "d:\\Temp";//轮询间隔 5 毫秒
long interval = 5l;//
FileAlterationObserver observer = newFileAlterationObserver(
rootDir,null,null);
observer.addListener(newMyFileListener());
FileAlterationMonitor monitor= newFileAlterationMonitor(interval,observer);//开始监控
monitor.start();
}
}------------------------------------------------------------------------------------------------------------------------------------------------------------jdk7 watchserviceimportjava.nio.file.FileSystem;importjava.nio.file.FileSystems;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.nio.file.WatchEvent;importjava.nio.file.WatchKey;importjava.nio.file.WatchService;importjava.util.HashMap;importjava.util.Map;import staticjava.nio.file.StandardWatchEventKinds.ENTRY_CREATE;import staticjava.nio.file.StandardWatchEventKinds.ENTRY_DELETE;public classTestWatchService {public static void main(String[] args) throwsException {final FileSystem fileSystem =FileSystems.getDefault();try (final WatchService watchService =fileSystem.newWatchService()) {final Map keyMap = new HashMap();final Path path = Paths.get("F:\\Test");
keyMap.put(path.register(watchService, ENTRY_CREATE, ENTRY_DELETE),path);
WatchKey watchKey;do{
watchKey=watchService.take();final Path eventDir =keyMap.get(watchKey);for (final WatchEvent>event : watchKey.pollEvents()) {final WatchEvent.Kind kind =event.kind();final Path eventPath =(Path) event.context();
System.out.println(eventDir+ ": " + event.kind() + ": " +event.context());
}
}while(watchKey.reset());
}
}
}------------------------------------------------------------------------------------------------------------------------------------------------------------jnotify
详见 http://www.blogjava.net/pengo/archive/2011/01/09/342622.html
jnotify分为64位 和 32位。 详见 http://jnotify.sourceforge.net/
public classTest {/***@paramargs*/
public static void main(String[] args) throwsException{//监控目录
String rootDir = "Z:\\";
String path=rootDir;int mask = JNotify.FILE_CREATED |JNotify.FILE_DELETED| JNotify.FILE_MODIFIED |JNotify.FILE_RENAMED;boolean watchSubtree = true;while (true) { //否则一次就完了
System.out.println("begin watch");
JNotify.addWatch(path, mask, watchSubtree,newMyJnotifyListner());
Thread.sleep(10000);
System.out.println("end watch");
}
}
}importnet.contentobjects.jnotify.JNotifyListener;public class MyJnotifyListner implementsJNotifyListener {public void fileRenamed(intwd, String rootPath, String oldName,
String newName) {
System.out.println("文件:" + rootPath + " : " + oldName + " 重命名为: "
+ newName + "\n");
}public void fileModified(intwd, String rootPath, String name) {
System.out.println("文件修改 " + rootPath + " : " + name + "\n");
}public void fileDeleted(intwd, String rootPath, String name) {
System.out.println("删除文件: " + rootPath + " : " + name + "\n");
}public void fileCreated(intwd, String rootPath, String name) {
System.out.println("新建文件: " + rootPath + " : " + name + "\n");
}
}