java使用WatchService監(jiān)控文件夾示例
通過(guò)java7提供的WatchService API 實(shí)現(xiàn)對(duì)文件夾的監(jiān)控
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(); //創(chuàng)建watchService path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); //注冊(cè)需要監(jiān)控的事件,ENTRY_CREATE 文件創(chuàng)建,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); //此處將處于等待狀態(tài),等待檢測(cè)到文件夾下得文件發(fā)生改變,返回WatchKey對(duì)象 if(watchKey != null){ List<WatchEvent<?>> events = watchKey.pollEvents(); //獲取所有得事件 for (WatchEvent event : events){ WatchEvent.Kind<?> kind = event.kind(); if (kind == StandardWatchEventKinds.OVERFLOW){ //當(dāng)前磁盤(pán)不可用 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()){ //已經(jīng)關(guān)閉了進(jìn)程 System.out.println("exit watch server"); break; } } } catch (InterruptedException e) { e.printStackTrace(); return; } } } }
就是這么簡(jiǎn)單就可以對(duì)一個(gè)文件夾進(jìn)行監(jiān)控了。
完整帶碼地址:WatchServerDemo_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java動(dòng)態(tài)修改配置即時(shí)生效的方式WatchService
- 使用maven構(gòu)建java9 service實(shí)例詳解
- Java ExecutorService四種線程池使用詳解
- Java編程Webservice指定超時(shí)時(shí)間代碼詳解
- Java使用WatchService監(jiān)控文件內(nèi)容變化的示例
- Java客戶端調(diào)用.NET的WebService實(shí)例
- java WSDL接口webService實(shí)現(xiàn)方式
- 詳解Java利用ExecutorService實(shí)現(xiàn)同步執(zhí)行大量線程
- java中Executor,ExecutorService,ThreadPoolExecutor詳解
- Java WebService 簡(jiǎn)單實(shí)例(附實(shí)例代碼)
- Java service層獲取HttpServletRequest工具類的方法
相關(guān)文章
SpringBoot通過(guò)@MatrixVariable進(jìn)行傳參詳解
這篇文章主要介紹了SpringBoot使用@MatrixVariable傳參,文章圍繞@MatrixVariable展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06常用Maven庫(kù),鏡像庫(kù)及maven/gradle配置(小結(jié))
這篇文章主要介紹了常用Maven庫(kù),鏡像庫(kù)及maven/gradle配置(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12springcloud連接遠(yuǎn)程nacos失敗顯示localhost服務(wù)連接失敗的問(wèn)題解決
這篇文章主要介紹了springcloud連接遠(yuǎn)程nacos失敗顯示localhost服務(wù)連接失敗的問(wèn)題解決,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家解決問(wèn)題有一定的幫助,需要的朋友可以參考下2024-03-03android顯示意圖激活另一個(gè)Activity的方法
下面小編就為大家?guī)?lái)一篇android顯示意圖激活另一個(gè)Activity的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06