zoukankan      html  css  js  c++  java
  • 下载时监听文件夹变化

    引自: https://www.cnblogs.com/yangzhilong/p/7487220.html

    package com.longge.mytest;
    
    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.WatchKey;
    import java.nio.file.WatchService;
    import java.util.List;
    
    /**
     * 测试JDK的WatchService监听文件变化
     * @author yangzhilong
     *
     */
    public class TestWatchService {
        public static void main(String[] args) throws IOException {
            // 需要监听的文件目录(只能监听目录)
            String path = "d:/test";
            
            WatchService watchService = FileSystems.getDefault().newWatchService();
            Path p = Paths.get(path);
            p.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY,  
                    StandardWatchEventKinds.ENTRY_DELETE,  
                    StandardWatchEventKinds.ENTRY_CREATE);  
            
            Thread thread = new Thread(() -> {
                try {  
                    while(true){  
                        WatchKey watchKey = watchService.take();  
                        List<WatchEvent<?>> watchEvents = watchKey.pollEvents();  
                        for(WatchEvent<?> event : watchEvents){  
                            //TODO 根据事件类型采取不同的操作。。。。。。。  
                            System.out.println("["+path+"/"+event.context()+"]文件发生了["+event.kind()+"]事件");    
                        }  
                        watchKey.reset();  
                    }  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }
            });
            thread.setDaemon(false);
            thread.start();
            
            // 增加jvm关闭的钩子来关闭监听
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                try {
                    watchService.close();
                } catch (Exception e) {
                }
            }));
        }
    }
    方法1

    引自: https://www.jianshu.com/p/928ce1010407

    package service;
    
    import config.Config;
    import java.io.IOException;
    import java.nio.file.*;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    public class WatchDirService {
        private WatchService watchService;
        private boolean notDone = true;
    
        public WatchDirService(String dirPath){
            init(dirPath);
        }
    
        private void init(String dirPath) {
            Path path = Paths.get(dirPath);
            try {
                watchService = FileSystems.getDefault().newWatchService();  //创建watchService
                path.register(watchService, 
                StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE); //注册需要监控的事件,ENTRY_CREATE 文件创建,ENTRY_MODIFY 文件修改,ENTRY_MODIFY 文件删除
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void start(){
            System.out.print("watch...");
            while (notDone){
                try {
                    WatchKey watchKey = watchService.poll(Config.POLL_TIME_OUT, TimeUnit.SECONDS); 
                    if(watchKey != null){
                        List<WatchEvent<?>> events = watchKey.pollEvents();  //获取所有得事件
                        for (WatchEvent event : events){
                            WatchEvent.Kind<?> kind = event.kind(); 
                            if (kind == StandardWatchEventKinds.OVERFLOW){
                                //当前磁盘不可用
                                continue;
                            }
                            WatchEvent<Path> ev = event;
                            Path path = ev.context();
                            if(kind == StandardWatchEventKinds.ENTRY_CREATE){
                                System.out.println("create " + path.getFileName());
                            }else if(kind == StandardWatchEventKinds.ENTRY_MODIFY){
                                System.out.println("modify " + path.getFileName());
                            }else if(kind == StandardWatchEventKinds.ENTRY_DELETE){
                                System.out.println("delete " + path.getFileName());
                            }
                        }
                        if(!watchKey.reset()){ 
                            //已经关闭了进程
                            System.out.println("exit watch server");
                            break;
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
    }
    
    作者:DrJasonZhang
    链接:https://www.jianshu.com/p/928ce1010407
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    方法2
  • 相关阅读:
    深入理解 ProtoBuf 原理与工程实践(概述)
    高性能缓存 Caffeine 原理及实战
    Java 多线程上下文传递在复杂场景下的实践
    SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
    MySQL 5.6.35 索引优化导致的死锁案例解析
    gitlab安装升级(大版本跨度9.4.5----13.2.1)
    mysql 查看表的索引
    python安装mysql库 ,MySQL-python
    Alpine包管理工具apk使用介绍
    docker容器添加hosts
  • 原文地址:https://www.cnblogs.com/cheese320/p/9152436.html
Copyright © 2011-2022 走看看