zoukankan      html  css  js  c++  java
  • jdk 1.7 监听文件目录

     package com;


    import java.io.IOException;
    import java.nio.file.FileSystem;
    import java.nio.file.FileSystems;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.WatchEvent;
    import java.nio.file.WatchKey;
    import java.nio.file.WatchService;
    import java.util.List;




    import static java.nio.file.StandardWatchEventKinds.*;
    public class Test {
        WatchService watch;
        
        public Test(Path path) throws IOException {
        watch = FileSystems.getDefault().newWatchService();
        path.register(watch,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
        }

        public void handleEvents() throws InterruptedException{
            while(true){
                WatchKey key = watch.take();
                for(WatchEvent event : key.pollEvents()){
                    WatchEvent.Kind kind = event.kind();


                    if(kind == OVERFLOW){//事件可能lost or discarded
                        continue;
                    }


                    WatchEvent e = (WatchEvent)event;
                    Path fileName = (Path) e.context();


                    System.out.printf("Event %s has happened,which fileName is %s%n"
                            ,kind.name(),fileName);

                }

               //如果事件invail

                if(!key.reset()){
                    break;
                }
            }
        }
        
        public static void main(String[] args) {
    try {
    new Test(Paths.get("F://")).handleEvents();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

  • 相关阅读:
    oracle中的exists 和not exists 用法详解
    再次谈谈easyui datagrid 的数据加载
    oracle之trunc(sysdate)
    小菜学习设计模式(五)—控制反转(Ioc)
    vim实用技巧
    003_Linux的Cgroup<实例详解>
    systemd在各个linux发行版的普及
    (部署新java程序,程序报错,需copy的一个包)——java使用siger 获取服务器硬件信息
    中国科学院国家授时中心
    Linux时间同步配置方法
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3001541.html
Copyright © 2011-2022 走看看