package com.jp.filemonitor; import org.apache.log4j.helpers.FileWatchdog; public class Log4jWatchdog { public static void main(String[] args) { GloablConfig gloablConfig = new GloablConfig("D:\\create_lo\\text.txt"); gloablConfig.setDelay(10000); gloablConfig.start(); while (true) { try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static class GloablConfig extends FileWatchdog{ protected GloablConfig(String filename) { super(filename); } @Override protected void doOnChange() { System.out.println(filename); } } }
2.common-io實現的文件夾變化的監聽git
這代碼網上不少,能夠搜索關鍵字FileAlterationMonitor,FileAlterationObserver FileAlterationObserver,這樣就能夠看到示例代碼了。github
Apacha common.io2.0提供了監聽文件變化的功能。apache
功能由三個組件組成。maven
監聽器 FileAlterationListeneride
用於實現文件改變時觸發的行爲。post
觀察者 FileAlterationObserverthis
用於觀察文件的改變,通知註冊的監聽器執行相應的事件。spa
監視器 FileAlterationMonitor線程
經過一線程,每間隔一段時間調用一次註冊的觀察者檢查文件。
maven依賴
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
FileAlterationListener
經過繼承FileAlterationListenerAdaptor,覆蓋相應事件方法。這裏只重寫了文件改變。還有其餘事件能夠查看FileAlterationListener接口看下。
public class FileAlterationReload extends FileAlterationListenerAdaptor { @Override public void onFileChange(File file) { System.out.println("文件改變"); } }
程序實現
public class Demo { public static void main(String[] args) throws Exception { //檢查classpath下properties文件夾下的properties文件。 FileAlterationObserver fileAlterationObserver = new FileAlterationObserver(Demo.class.getClassLoader().getResource("properties").getPath(),new PropertiesFileFilter()); FileAlterationListener fileAlterationListener =new FileAlterationReload(); //註冊監聽器 fileAlterationObserver.addListener(fileAlterationListener); FileAlterationMonitor fileAlterationMonitor = new FileAlterationMonitor(); //註冊觀察者 fileAlterationMonitor.addObserver(fileAlterationObserver); //啓動監聽 fileAlterationMonitor.start(); //讓主線程別這麼快結束。 Thread.sleep(1000000); } }
修改properties文件夾下的properties文件時,會輸出 文件改變。
應用場景
能夠用於實現配置文件的熱部署
例子:
https://github.com/wujiazhen2/DynamicConfigRefresh
3.nio實現文件夾內容變化的監聽
package com.jp.filemonitor; 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; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ResourceListener { private static ExecutorService fixedThreadPool = Executors.newCachedThreadPool(); private WatchService ws; private String listenerPath; private ResourceListener(String path) { try { ws = FileSystems.getDefault().newWatchService(); this.listenerPath = path; start(); } catch (IOException e) { e.printStackTrace(); } } private void start() { fixedThreadPool.execute(new Listner(ws,this.listenerPath)); } public static void addListener(String path) throws IOException { ResourceListener resourceListener = new ResourceListener(path); Path p = Paths.get(path); p.register(resourceListener.ws,StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_CREATE); } public static void main(String[] args) throws IOException { ResourceListener.addListener("F:\\"); ResourceListener.addListener("d:\\"); } } class Listner implements Runnable { private WatchService service; private String rootPath; public Listner(WatchService service,String rootPath) { this.service = service; this.rootPath = rootPath; } public void run() { try { while(true){ WatchKey watchKey = service.take(); List<WatchEvent<?>> watchEvents = watchKey.pollEvents(); for(WatchEvent<?> event : watchEvents){ //TODO 根據事件類型採起不一樣的操做。。。。。。。 System.out.println("["+rootPath+event.context()+"]文件發生了["+event.kind()+"]事件"+ event.count()); } watchKey.reset(); } } catch (InterruptedException e) { e.printStackTrace(); }finally{ System.out.println("fdsfsdf"); try { service.close(); } catch (IOException e) { e.printStackTrace(); } } } }