JDK7目录监视服务

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
import java.io.IOException;
import java.nio.file.*;

public class DirectoryWatcher {
    public static void main(String[] args) throws IOException, InterruptedException {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        Path path = Paths.get("F:", "share").toAbsolutePath();
        path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE);
        while(true) {
            WatchKey watchKey = watchService.take();
            for (WatchEvent event : watchKey.pollEvents()) {
                if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                    System.out.println("Create " + path.resolve((Path) event.context()).toAbsolutePath());
                } else if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                    System.out.println("Modify " + path.resolve((Path) event.context()).toAbsolutePath());
                } else {
                    System.out.println("Delete " + path.resolve((Path) event.context()).toAbsolutePath());
                }
            }
            watchKey.reset();

            // Cancel the watch
            // watchKey.cancel();
        }
    }
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:iOS通过访问系统通讯录,获取选择用户的全名和电话

下一篇:objective-c 将字符转换为键盘码的代码