引自: 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) { } })); } }
引自: 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 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。