2013-10-30 29 views
1

我使用下面的方法使用Java 7 nio WatchService來看一個目錄。在一個目錄內發生的捕獲事件

Path myDir = Paths.get("/rootDir"); 

try { 
    WatchService watcher = myDir.getFileSystem().newWatchService(); 
    myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, 
    StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); 

    WatchKey watckKey = watcher.take(); 

    List<WatchEvent<?>> events = watckKey.pollEvents(); 
    for (WatchEvent event : events) { 
    if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { 
     System.out.println("Created: " + event.context().toString()); 
     JOptionPane.showMessageDialog(null,"Created: " + event.context().toString()); 
    } 
    if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { 
     System.out.println("Delete: " + event.context().toString()); 
     JOptionPane.showMessageDialog(null,"Delete: " + event.context().toString()); 
    } 
    if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) { 
     System.out.println("Modify: " + event.context().toString()); 
     JOptionPane.showMessageDialog(null,"Modify: " + event.context().toString()); 
    } 
    } 

} catch (Exception e) { 
    System.out.println("Error: " + e.toString()); 
} 

但是,上述方法只響應一個事件發生在目錄後,觀察者沒有響應該文件夾中發生的事件。有沒有辦法可以修改這個來捕獲文件夾內發生的所有事件。我也想修改它來捕獲子文件夾中發生的事件。有人可以幫助我。

謝謝。

回答

1

使用Apache下議院IO文件監視
還將囊括事件子文件夾中發生得

import java.io.File; 
import java.io.IOException; 

import org.apache.commons.io.monitor.FileAlterationListener; 
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor; 
import org.apache.commons.io.monitor.FileAlterationMonitor; 
import org.apache.commons.io.monitor.FileAlterationObserver; 

public class Monitor { 

    public Monitor() { 

    } 

    //path to a folder you are monitoring . 

    public static final String FOLDER = MYPATH; 


    public static void main(String[] args) throws Exception { 
     System.out.println("monitoring started"); 
     // The monitor will perform polling on the folder every 5 seconds 
     final long pollingInterval = 5 * 1000; 

     File folder = new File(FOLDER); 

     if (!folder.exists()) { 
      // Test to see if monitored folder exists 
      throw new RuntimeException("Directory not found: " + FOLDER); 
     } 

     FileAlterationObserver observer = new FileAlterationObserver(folder); 
     FileAlterationMonitor monitor = 
       new FileAlterationMonitor(pollingInterval); 
     FileAlterationListener listener = new FileAlterationListenerAdaptor() { 
      // Is triggered when a file is created in the monitored folder 
      @Override 
      public void onFileCreate(File file) { 

        // "file" is the reference to the newly created file 
        System.out.println("File created: "+ file.getCanonicalPath()); 


      } 

      // Is triggered when a file is deleted from the monitored folder 
      @Override 
      public void onFileDelete(File file) { 
       try { 
        // "file" is the reference to the removed file 
        System.out.println("File removed: "+ file.getCanonicalPath()); 
        // "file" does not exists anymore in the location 
        System.out.println("File still exists in location: "+ file.exists()); 
       } catch (IOException e) { 
        e.printStackTrace(System.err); 
       } 
      } 
     }; 

     observer.addListener(listener); 
     monitor.addObserver(observer); 
     monitor.start(); 
    } 
} 
+0

這是捕獲像一個文本文件中改變內容的事件? – Sajirupee

+0

不,它會檢查是否在給定的目錄中創建或刪除任何新文件 –

+0

它還將檢查是否有任何新文件在子文件夾 –

5

the JavaDoc of WatchService

甲必看對象通過調用其寄存器方法,返回一個WatchKey來表示註冊用表的服務登記。當一個對象的事件被檢測到時,該鍵被髮信號通知,並且如果當前沒有信號通知,則它被排隊等待監視服務,以便消費者可以調用它來調用輪詢或採取方法來檢索鍵和處理事件。一旦處理完事件,消費者調用密鑰的重置方法來重置密鑰,從而允許密鑰被髮信號通知並重新排隊以進一步發生事件。

您只能撥打watcher.take()一次。

要觀看更多活動,您必須在使用WatchEvent之後致電watchKey.reset()。把所有這些放在一個循環中。

while (true) { 
    WatchKey watckKey = watcher.take(); 
    List<WatchEvent<?>> events = watckKey.pollEvents(); 
    for (WatchEvent event : events) { 
    // process event 
    } 
    watchKey.reset(); 
} 

也看看the relevant section of the Java Tutorial

+0

這應該是公認的答案 –

相關問題