zoukankan      html  css  js  c++  java
  • java7 NIO2 watching service API

    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);
            }
    
        }
    
    }
  • 相关阅读:
    NHibernate初学遇到的问题及解决方案之一
    MSSQL2005数据库备份时提示:System.Data.SqlClient.SqlError: 媒体集有 2 个媒体簇,但只提供了 1 个。 .
    VS自带的ASP.NET web服务器Cassini源代码
    Python开发WebService使用soaplib库
    vue入门创建项目
    vue回顾Object.defineproperty方法
    虚拟机ping不通百度的解决方案
    zookeeper完全分布式环境部署
    vue+echarts生成图表步骤
    虚拟机时间同步
  • 原文地址:https://www.cnblogs.com/interdrp/p/4867998.html
Copyright © 2011-2022 走看看