java目录监控_Java高效监控文件目录

本文介绍了三种在Java中监控文件目录的方法:1) Apache Commons IO,适用于小文件夹,但在大目录下效率较低;2) Java 7的Watch Service,实际应用中响应速度不佳;3) jnotify,直接调用系统API,速度快且简单,推荐使用。详细代码示例展示了如何实现这三种监控方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有三种方式: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");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值