jdk7监听文件系统变化通知新特性
2017-11-08 17:58
555 查看
2.1IO and New IO 监听文件系统变化通知
通过FileSystems.getDefault().newWatchService()获取watchService,然后将需要监听的path目录注册到这个watchservice中,对于这个目录的文件修改,新增,删除等实践可以配置,然后就自动能监听到响应的事件。
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;
}
通过FileSystems.getDefault().newWatchService()获取watchService,然后将需要监听的path目录注册到这个watchservice中,对于这个目录的文件修改,新增,删除等实践可以配置,然后就自动能监听到响应的事件。
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;
}
相关文章推荐
- JDK7新特性之监听文件系统更改
- inotify-java linux系统监听文件发生变化,实时通知java程序
- jdk7 新特性 监听文件目录变化
- jdk7监听文件变化感知
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- JDK7的WatchServide监听文件系统
- Android使用ContentObserver监听数据库变化,截取系统通知用户接收到短信
- 用Linux 2.6内核中的文件系统变化通知机制inotify可实现跨机文件同步
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- (转)FindFirstChangeNotification,创建一个文件通知对象,该对象用于监视文件系统发生的变化
- Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
- notify -- Linux 2.6 内核中的文件系统变化通知机制
- Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- inotify -- Linux 2.6 内核中的文件系统变化通知机制
- WatchService API实现监听文件系统中的文件变化(包括各级子目录)