Java7 WatchService 监控文件变化
2015-04-20 23:38
423 查看
package com.test.watchservice;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class TestWatchService {
public static void main(String[] args) {
//获取文件系统的WatchService 对象
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
//为指定目录路径注册舰艇
Paths.get("E:\\").register(watchService, StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE);
while(true){
//获取下一个文件变化事件
try {
WatchKey key = watchService.take();
for (WatchEvent<?> enent : key.pollEvents()) {
System.out.println(enent.context()+"文件发生了"+enent.kind()+"事件");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class TestWatchService {
public static void main(String[] args) {
//获取文件系统的WatchService 对象
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
//为指定目录路径注册舰艇
Paths.get("E:\\").register(watchService, StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE);
while(true){
//获取下一个文件变化事件
try {
WatchKey key = watchService.take();
for (WatchEvent<?> enent : key.pollEvents()) {
System.out.println(enent.context()+"文件发生了"+enent.kind()+"事件");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- NIO--WatchService、WatchKey(监控文件变化)
- NIO.2:WatchService、WatchKey(监控文件变化)
- java7 WatchServiceDemo 文件系统监控
- [疯狂Java]NIO.2:WatchService、WatchKey(监控文件变化)
- JAVA 文件监控 WatchService的示例方法
- watchService 监控文件生产数量的思路
- 使用java.nio.file.WatchService监控文件夹里的文件再操作文件出现的“文件不存在或目录不存在”的错误
- java利用WatchService实时监控某个目录下的文件变化并按行解析(注:附源代码)
- 使用WatchService类做文件监控总结
- 利用WatchService监听文件变化
- WatchService API实现监听文件系统中的文件变化(包括各级子目录)
- WatchService——监控硬盘文件改动功能用法及其缺陷
- 文件变化监视 java.nio.file.WatchService
- JAVA 文件监控 WatchService
- Java利用WatchService监听文件变化示例
- linux监控文件变化(watch及tail命令)
- Python for Windows——监控Windows某个目录下文件的变化
- C# 利用FTP自动下载xml文件后利用 FileSystemWatcher 监控目录下文件变化并自动更新数据库
- Powershell 之监控文件系统变化
- redis实现分布式锁——核心 setx+pipe watch监控key变化-事务