package com.uv;
/*
 * @author uv
 * @date 2018/12/19 17:37
 */

import java.io.File;
import java.nio.file.FileSystems;
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.HashSet;
import java.util.Set;


public class WatchFile {

    private static WatchService watchService;
    private static String filePath;

    public void watch() {

        // conf目录全路径
        filePath = (new File(\"d:\\\\data\").getAbsolutePath());

        try {
            // 获取文件系统的WatchService对象
            watchService = FileSystems.getDefault().newWatchService();
            // 监听filePath目录下文件是否修改或增加;register()方法后面监听事件种类还可以增加、删除。
            Paths.get(filePath).register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE); //监听修改、添加
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 设置后台线程
        Thread watchThread = new Thread(new Runnable() {

            private Set<String> fileNameSet = new HashSet<>(); //Set去重,当添加文件的时候,修改时间也会被监听执行

            @Override
            public void run() {
                while (true) {
                    try {
                        // 获取下一个文件改动事件
                        WatchKey mkey = watchService.take();
                        for (WatchEvent<?> event : mkey.pollEvents()) {
                            fileNameSet.add(event.context() + \"\");
                        }
                        for (String name : fileNameSet) {
                            System.out.println(\"文件:\" + filePath + \"/\" + name);
                        }
                        // 重设WatchKey
                        boolean valid = mkey.reset();
                        fileNameSet.clear();

                        // 如果重设失败,退出监听
                        if (!valid) {
                            break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        // 设置为后台守护线程
        watchThread.setDaemon(true);
        watchThread.start();

    }

    /**
     * 模拟测试
     */
    public static void main(String[] args) throws Exception {

        // 开启监听程序,如有改动,则更新对象
        WatchFile w = new WatchFile();
        w.watch();

        // 假装做一些事情,延迟。
        Thread.sleep(8000000000000l);

    }

}

 

收藏 打印