JDK7新特性<六> 监听文件系统的更改
2011-12-14 17:16
399 查看
我们用IDE(例如Eclipse)编程,外部更改了代码文件,IDE马上提升“文件有更改”。Jdk7的NIO2.0也提供了这个功能,用于监听文件系统的更改。它采用类似观察者的模式,注册相关的文件更改事件(新建,删除……),当事件发生的,通知相关的监听者。
java.nio.file.*包提供了一个文件更改通知API,叫做Watch Service API.
实现流程如下
1.为文件系统创建一个WatchService 实例 watcher
2.为你想监听的目录注册 watcher。注册时,要注明监听那些事件。
3.在无限循环里面等待事件的触发。当一个事件发生时,key发出信号,并且加入到watcher的queue
4.从watcher的queue查找到key,你可以从中获取到文件名等相关信息
5.遍历key的各种事件
6.重置 key,重新等待事件
7.关闭服务
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/c375bdf5460d8e816187b5f12ec8a477.png)
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import static java.nio.file.StandardWatchEventKind.*;
/**
* @author kencs@foxmail.com
*/
public class TestWatcherService {
private WatchService watcher;
public TestWatcherService(Path path)throws IOException{
watcher = FileSystems.getDefault().newWatchService();
path.register(watcher, ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
}
public void handleEvents() throws InterruptedException{
while(true){
WatchKey key = watcher.take();
for(WatchEvent<?> event : key.pollEvents()){
WatchEvent.Kind kind = event.kind();
if(kind == OVERFLOW){//事件可能lost or discarded
continue;
}
WatchEvent<Path> e = (WatchEvent<Path>)event;
Path fileName = e.context();
System.out.printf("Event %s has happened,which fileName is %s%n"
,kind.name(),fileName);
}
if(!key.reset()){
break;
}
}
}
public static void main(String args[]) throws IOException, InterruptedException{
if(args.length!=1){
System.out.println("请设置要监听的文件目录作为参数");
System.exit(-1);
}
new TestWatcherService(Paths.get(args[0])).handleEvents();
}
}
接下来,见证奇迹的时刻
命令行信息代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/c375bdf5460d8e816187b5f12ec8a477.png)
Event ENTRY_CREATE has happened,which fileName is 新建文本文档.txt
Event ENTRY_DELETE has happened,which fileName is 新建文本文档.txt
Event ENTRY_CREATE has happened,which fileName is abc.txt
Event ENTRY_MODIFY has happened,which fileName is abc.txt
Event ENTRY_MODIFY has happened,which fileName is abc.txt
java.nio.file.*包提供了一个文件更改通知API,叫做Watch Service API.
实现流程如下
1.为文件系统创建一个WatchService 实例 watcher
2.为你想监听的目录注册 watcher。注册时,要注明监听那些事件。
3.在无限循环里面等待事件的触发。当一个事件发生时,key发出信号,并且加入到watcher的queue
4.从watcher的queue查找到key,你可以从中获取到文件名等相关信息
5.遍历key的各种事件
6.重置 key,重新等待事件
7.关闭服务
Java代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/c375bdf5460d8e816187b5f12ec8a477.png)
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import static java.nio.file.StandardWatchEventKind.*;
/**
* @author kencs@foxmail.com
*/
public class TestWatcherService {
private WatchService watcher;
public TestWatcherService(Path path)throws IOException{
watcher = FileSystems.getDefault().newWatchService();
path.register(watcher, ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
}
public void handleEvents() throws InterruptedException{
while(true){
WatchKey key = watcher.take();
for(WatchEvent<?> event : key.pollEvents()){
WatchEvent.Kind kind = event.kind();
if(kind == OVERFLOW){//事件可能lost or discarded
continue;
}
WatchEvent<Path> e = (WatchEvent<Path>)event;
Path fileName = e.context();
System.out.printf("Event %s has happened,which fileName is %s%n"
,kind.name(),fileName);
}
if(!key.reset()){
break;
}
}
}
public static void main(String args[]) throws IOException, InterruptedException{
if(args.length!=1){
System.out.println("请设置要监听的文件目录作为参数");
System.exit(-1);
}
new TestWatcherService(Paths.get(args[0])).handleEvents();
}
}
接下来,见证奇迹的时刻
1.随便新建一个文件夹 例如 c:\\test
2.运行程序 java TestWatcherService c:\\test
3.在该文件夹下新建一个文件本件 “新建文本文档.txt”
4.将上述文件改名为 “abc.txt”
5.打开文件,输入点什么吧,再保存。
6.Over!看看命令行输出的信息吧
命令行信息代码
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/c375bdf5460d8e816187b5f12ec8a477.png)
Event ENTRY_CREATE has happened,which fileName is 新建文本文档.txt
Event ENTRY_DELETE has happened,which fileName is 新建文本文档.txt
Event ENTRY_CREATE has happened,which fileName is abc.txt
Event ENTRY_MODIFY has happened,which fileName is abc.txt
Event ENTRY_MODIFY has happened,which fileName is abc.txt
相关文章推荐
- 探秘JDK7新特性之监听文件系统的更改
- JDK7新特性&nbsp;监听文件系统的更改
- [转]JDK7新特性 监听文件系统的更改
- JDK7新特性之监听文件系统更改
- Solaris更改文件系统大小
- 通过更改字库文件组件及相关设置实现对NK进行裁剪(尤其适用中文简体系统)
- flume实例二、监听目录日志上传到HDFS文件系统
- Solaris更改文件系统大小
- 基本文件管理,针对Centos7的XFS文件系统备份恢复(需要更改)
- 使用commons-vfs监听文件系统
- 修改BOOT文件。更改系统启动菜单的时间
- Solaris如何更改文件系统大小?更改Solaris文件系统大小
- rhel6文件系统属性更改相关操作
- FATfs文件系统移植-文件更改记录
- 启动文件系统时显示内核分区:FTL header not found 的错误更改
- Linux怎么更改文件的所有者、所属组?以及怎么更改系统创建的默认权限?
- JDK7新特性<四> NIO2.0 文件系统
- Sdcard插拔、状态广播监听,Android文件系统,Android存储器相关知识总结
- sharepoint 2010 应用程序试图执行安全策略不允许的操作。要授予此应用程序所需的权限,请与系统管理员联系,或在配置文件中更改该应用程序的信任级别。
- 数据库MySQL免安装版本在Windows系统上更改数据文件目录