java7 NIO2新增了文件系统的相关事件处理API,为目录,文件新增修改删除等事件添加事件处理。

package reyo.sdk.utils.file;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchKey;
import java.nio.file.WatchService; public class NIO2WatchService { // WatchService 是线程安全的,跟踪文件事件的服务,一般是用独立线程启动跟踪
public void watchRNDir(Path path) throws IOException, InterruptedException {
try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
// 给path路径加上文件观察服务
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
// start an infinite loop
while (true) {
// retrieve and remove the next watch key
final WatchKey key = watchService.take();
// get list of pending events for the watch key
for (WatchEvent<?> watchEvent : key.pollEvents()) {
// get the kind of event (create, modify, delete)
final Kind<?> kind = watchEvent.kind();
// handle OVERFLOW event
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
// 创建事件
if (kind == StandardWatchEventKinds.ENTRY_CREATE) { }
// 修改事件
if (kind == StandardWatchEventKinds.ENTRY_MODIFY) { }
// 删除事件
if (kind == StandardWatchEventKinds.ENTRY_DELETE) { }
// get the filename for the event
@SuppressWarnings("unchecked")
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
final Path filename = watchEventPath.context();
// print it out
System.out.println(kind + " -> " + filename); }
// reset the keyf
boolean valid = key.reset();
// exit loop if the key is not valid (if the directory was
// deleted, for
if (!valid) {
break;
}
}
}
} /**
* @param args
*/
public static void main(String[] args) { // String sourceDir = "D:\\temp";
//
// Vector<String> v1 = FileOptionUtil.listAllDirAndFile(sourceDir,
// sourceDir, true);
//
// for (int i = 0; i < v1.size(); i++) {
// String fileNameb = v1.get(i);
// if (fileNameb.indexOf(".") != -1) {
// fileNameb = FileOptionUtil.getFileNameNoEx(fileNameb).substring(0,
// FileOptionUtil.getFileNameNoEx(fileNameb).length() - 14) + "." +
// FileOptionUtil.getExtensionName(fileNameb);
// } else {
// fileNameb = fileNameb.substring(0, fileNameb.length() - 14);
// }
//
// } final Path path = Paths.get("D:\\temp");
NIO2WatchService watch = new NIO2WatchService();
try {
watch.watchRNDir(path);
} catch (IOException | InterruptedException ex) {
System.err.println(ex);
} } }

最新文章

  1. JNI相关知识
  2. java轻量级Http Server
  3. CORS(跨域资源共享)
  4. CSS中font-size、font-family、line-height顺序以及简写属性
  5. Go语言的类型转化
  6. table表格的属性
  7. 怎样解决VirtrualBox不能新建64bit的系统的问题
  8. C语言基础知识-循环结构
  9. javascript-代码复用模式
  10. Reflow、Repaint 性能优化
  11. WINDOWS BITLOCK
  12. MVC验证04-自定义验证规则、日期范围验证
  13. 说说ajax上传数据和接收数据
  14. 利用layer实现MVC页面数据互交提示弹框
  15. for循环去重排序
  16. 如何用Python爬虫实现百度图片自动下载?
  17. mybatis的mapper接口代理使用的三个规范
  18. window.location.href 传参中文乱码问题!!!
  19. RabbitMQ消息队列(五)-安装amqp扩展并订阅/发布Demo(.Net Core版)
  20. P1041 传染病控制(dfs)

热门文章

  1. Ubuntu 12.04 下 Sublime Text 3 Build 3047 破解
  2. IOC入门
  3. Sqlserver中PIVOT行转列透视操作
  4. .NetCore下B/S结构 初探基于遗传学算法的中学自动排课走班(二)
  5. HTML5练习3
  6. R语言编程艺术(3)R语言编程基础
  7. rabbitmq学习(五) —— 路由
  8. JAVAEE——SpringBoot日志篇:日志框架SLF4j、日志配置、日志使用、切换日志框架
  9. SCTF2018-Event easiest web - phpmyadmin
  10. vue-video-player的使用总结