自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

探秘JDK7新特性之監(jiān)聽文件系統(tǒng)的更改

開發(fā) 后端
本篇文章主要介紹JDK7的新特新-監(jiān)聽文件系統(tǒng)的更改的相關(guān)信息,希望對大家有所幫助。

我們用IDE(例如Eclipse)編程,外部更改了代碼文件,IDE馬上提升“文件有更改”。Jdk7的NIO2.0也提供了這個功能,用于監(jiān)聽文件系統(tǒng)的更改。它采用類似觀察者的模式,注冊相關(guān)的文件更改事件(新建,刪除……),當事件發(fā)生的,通知相關(guān)的監(jiān)聽者。

java.nio.file.*包提供了一個文件更改通知API,叫做Watch Service API.

實現(xiàn)流程如下

1.為文件系統(tǒng)創(chuàng)建一個WatchService 實例 watcher

2.為你想監(jiān)聽的目錄注冊 watcher。注冊時,要注明監(jiān)聽那些事件。

3.在無限循環(huán)里面等待事件的觸發(fā)。當一個事件發(fā)生時,key發(fā)出信號,并且加入到watcher的queue

4.從watcher的queue查找到key,你可以從中獲取到文件名等相關(guān)信息

5.遍歷key的各種事件

6.重置 key,重新等待事件

7.關(guān)閉服務(wù)

Java代碼

  1. import java.io.IOException;     
  2. import java.nio.file.FileSystems;     
  3. import java.nio.file.Path;     
  4. import java.nio.file.Paths;     
  5. import java.nio.file.WatchEvent;     
  6. import java.nio.file.WatchKey;     
  7. import java.nio.file.WatchService;     
  8. import static java.nio.file.StandardWatchEventKind.*;     
  9.     
  10. /**    
  11.  * @author kencs@foxmail.com    
  12.  */    
  13. public class TestWatcherService {     
  14.          
  15.     private WatchService watcher;     
  16.          
  17.     public TestWatcherService(Path path)throws IOException{     
  18.         watcher = FileSystems.getDefault().newWatchService();     
  19.         path.register(watcher, ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);     
  20.     }     
  21.          
  22.     public void handleEvents() throws InterruptedException{     
  23.         while(true){     
  24.             WatchKey key = watcher.take();     
  25.             for(WatchEvent event : key.pollEvents()){     
  26.                 WatchEvent.Kind kind = event.kind();     
  27.                      
  28.                 if(kind == OVERFLOW){//事件可能lost or discarded     
  29.                     continue;     
  30.                 }     
  31.                      
  32.                 WatchEvent e = (WatchEvent)event;     
  33.                 Path fileName = e.context();     
  34.                      
  35.                 System.out.printf("Event %s has happened,which fileName is %s%n"    
  36.                         ,kind.name(),fileName);     
  37.             }     
  38.             if(!key.reset()){     
  39.                 break;     
  40.             }     
  41.         }     
  42.     }     
  43.          
  44.     public static void main(String args[]) throws IOException, InterruptedException{     
  45.         if(args.length!=1){     
  46.             System.out.println("請設(shè)置要監(jiān)聽的文件目錄作為參數(shù)");     
  47.             System.exit(-1);     
  48.         }     
  49.         new TestWatcherService(Paths.get(args[0])).handleEvents();     
  50.     }     
  51. }   

 

接下來,見證奇跡的時刻

1.隨便新建一個文件夾 例如 c:\\test

2.運行程序 java TestWatcherService c:\\test

3.在該文件夾下新建一個文件本件 “新建文本文檔.txt”

4.將上述文件改名為 “abc.txt”

5.打開文件,輸入點什么吧,再保存。

6.Over!看看命令行輸出的信息吧

命令行信息代碼

  1. Event ENTRY_CREATE has happened,which fileName is 新建文本文檔.txt     
  2. Event ENTRY_DELETE has happened,which fileName is 新建文本文檔.txt     
  3. Event ENTRY_CREATE has happened,which fileName is abc.txt     
  4. Event ENTRY_MODIFY has happened,which fileName is abc.txt     
  5. Event ENTRY_MODIFY has happened,which fileName is abc.txt    

【編輯推薦】

  1. NetBeans 7.0公布路線圖 將針對JDK 7進行更新
  2. NetBeans 6.10 M1發(fā)布 增強WebLogic支持
  3. Java 7將于明年7月28日正式發(fā)布面向開發(fā)者
  4. Java 7,一個技術(shù)標準的商業(yè)咒語
  5. Java 7 未按時發(fā)布 計劃再次延期
責任編輯:金賀 來源: ITEYE博客
相關(guān)推薦

2011-05-20 09:43:23

JDK7

2011-05-20 09:35:22

JDK7

2011-05-20 10:28:29

JDK7

2011-05-20 09:59:42

JDK7

2011-05-20 09:53:00

JDK7

2011-05-20 10:15:06

JDK7

2011-07-29 09:31:32

JDK 7

2010-06-22 11:07:42

JDK 7Java開發(fā)Java

2010-06-28 09:26:15

JDK 7Swing組件Java

2010-06-23 09:25:50

JDK 7Java開發(fā)Java

2009-11-23 19:50:12

PHP6.0

2010-06-29 09:23:09

JDK 7I|ONIO.2

2012-05-21 09:48:58

Ext4

2011-03-07 09:11:23

2010-05-19 15:23:51

Linux 2.6.3Ceph分布式文件系統(tǒng)

2011-03-24 09:22:36

Java 7JDBC4

2009-07-03 08:37:07

微軟Windows 7WinFS

2021-05-31 07:50:59

Linux文件系統(tǒng)

2021-05-06 20:03:00

JavaStream代碼

2017-04-20 14:55:36

LinuxPyinotifyPython
點贊
收藏

51CTO技術(shù)棧公眾號