2011-02-09 35 views
17

Tôi có một ứng dụng mà tôi muốn nghe bất kỳ thay đổi nào được thực hiện cho một thư mục cụ thể. Ứng dụng sẽ ping cho tôi ngay khi có bất kỳ tệp nào được thêm, xóa hoặc cập nhật trong thư mục đó.Trình nghe thư mục trong Java

Trả lời

21

Bạn có thể sử dụng JNotify

JNotify là một thư viện java cho phép ứng dụng java để lắng nghe để nộp sự kiện hệ thống, chẳng hạn như: File tạo tập tin sửa đổi tập tin đổi tên file bị xóa được hỗ trợ nền tảng

Ghi chú Windows (2000 hoặc mới hơn) Windows Linux với hỗ trợ INofity (2.6.14 hoặc mới hơn) Linux ghi chú Mac OS X (1 0,5 hoặc mới hơn) Mac OS lưu ý

More Info:

Tải JNotify từ here

Giải nén zip, đưa .dll/.so theo nền tảng trong đường dẫn thư mục lib của bạn. và tạo lớp học cung cấp jnotify-0.93.jar trong đường dẫn lớp học.

Mẫu mã:

package org.life.java.stackoverflow.questions; 

import net.contentobjects.jnotify.JNotify; 
import net.contentobjects.jnotify.JNotifyListener; 

/** 
* 
* @author Jigar 
*/ 
public class JNotifyDemo { 

    public void sample() throws Exception { 
     // path to watch 
     String path = System.getProperty("user.home"); 

     // watch mask, specify events you care about, 
     // or JNotify.FILE_ANY for all events. 
     int mask = JNotify.FILE_CREATED 
       | JNotify.FILE_DELETED 
       | JNotify.FILE_MODIFIED 
       | JNotify.FILE_RENAMED; 

     // watch subtree? 
     boolean watchSubtree = true; 

     // add actual watch 
     int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener()); 

     // sleep a little, the application will exit if you 
     // don't (watching is asynchronous), depending on your 
     // application, this may not be required 
     Thread.sleep(1000000); 

     // to remove watch the watch 
     boolean res = JNotify.removeWatch(watchID); 
     if (!res) { 
      // invalid watch ID specified. 
     } 
    } 

    class Listener implements JNotifyListener { 

     public void fileRenamed(int wd, String rootPath, String oldName, 
       String newName) { 
      print("renamed " + rootPath + " : " + oldName + " -> " + newName); 
     } 

     public void fileModified(int wd, String rootPath, String name) { 
      print("modified " + rootPath + " : " + name); 
     } 

     public void fileDeleted(int wd, String rootPath, String name) { 
      print("deleted " + rootPath + " : " + name); 
     } 

     public void fileCreated(int wd, String rootPath, String name) { 
      print("created " + rootPath + " : " + name); 
     } 

     void print(String msg) { 
      System.err.println(msg); 
     } 
    } 
    public static void main(String[] args) throws Exception { 
     new JNotifyDemo().sample(); 
    } 
} 

Output:

modified C:\Documents and Settings\jigar: LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop 
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_4s8ywsvyukghK0uDxRop 
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default 
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea9 
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae 
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom 
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eae 
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\output1295531079119 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Current Session 
deleted C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001ea8 
created C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache 
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom 
modified C:\Documents and Settings\jigar : LOCALS~1\Temp\etilqs_04gchL79ZJrpClZIqiom 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf 
modified C:\Documents and Settings\jigar : Local Settings\Application Data\Google\Chrome\User Data\Default\Cache\f_001eaf 
+11

* Downvoters * xin vui lòng bình luận –

+1

Khi tôi chạy chương trình nó mang lại cho tôi exceptionError tải thư viện, java.library.path = C : \ Program Files \ Java \ jdk1.7.0 \ bin;.; C: \ Windows \ Sun \ Java \ bin; C: \ Windows \ system32; C: \ Windows; C: \ Windows; C: \ Windows \ system32; C: \ Windows \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; C: \ Program Files \ Microsoft SQL Server \ 90 \ Công cụ \ binn \; C: \ apache-tomcat-6.0.26 \ bin; C: \ Tệp chương trình \ Java \ jdk1.7.0 \ bin; C: \ Program Files \ TortoiseSVN \ bin; C: \ Program Files \ putty; C: \ Program Files \ Google \ Chrome \ Application; C: \ Program Files \ Java \ jdk1.7.0 \ include Ngoại lệ trong chuỗi "main" java.lang.Unsa – Jinith

+0

'Giải nén zip, đặt .dll/.so theo nền tảng trong đường dẫn lib của bạn.' –

3

Chú thích thông báo tệp trong java. Mã mẫu

public void sample() throws Exception { 
     // path to watch  
     String path = System.getProperty("user.home");  
     // watch mask, specify events you care about,  
     // or JNotify.FILE_ANY for all events.  
     int mask = JNotify.FILE_CREATED |     
     JNotify.FILE_DELETED |     
     JNotify.FILE_MODIFIED |     
     JNotify.FILE_RENAMED;  
     // watch subtree? boolean watchSubtree = true;  
     // add actual watch  
     int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());  
     // sleep a little, the application will exit if you  
     // don't (watching is asynchronous), depending on your  
     // application, this may not be required  
     Thread.sleep(1000000);  
     // to remove watch the watch  
     boolean res = JNotify.removeWatch(watchID);  
     if (!res) {  
      // invalid watch ID specified.  
      } 
     } 
    class Listener implements JNotifyListener 
    {  
     public void fileRenamed(int wd, String rootPath, String oldName,   
       String newName) {  
      print("renamed " + rootPath + " : " + oldName + " -> " + newName); }  
     public void fileModified(int wd, String rootPath, String name) 
     {  print("modified " + rootPath + " : " + name); }  
     public void fileDeleted(int wd, String rootPath, String name) {  
      print("deleted " + rootPath + " : " + name); }  
     public void fileCreated(int wd, String rootPath, String name) {  
      print("created " + rootPath + " : " + name); }  
     void print(String msg) {  
      System.err.println(msg); } 
     } 
+0

Khi tôi chạy chương trình nó mang lại cho tôi exceptionError tải thư viện, java.library.path = C: \ Program Files \ Java \ jdk1.7.0 \ bin;.; C: \ Windows \ Sun \ Java \ bin; C: \ Windows \ system32; C: \ Windows; C: \ Windows; C: \ Windows \ system32; C: \ Windows \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; C: \ Program Files \ Microsoft SQL Server \ 90 \ Công cụ \ binn \; C: \ apache-tomcat-6.0.26 \ bin; C: \ Program Files \ Java \ jdk1 .7.0 \ bin; C: \ Program Files \ TortoiseSVN \ bin; C: \ Program Files \ putty; C: \ Program Files \ Google \ Chrome \ Application; C: \ Program Files \ Java \ jdk1.7.0 \ include Ngoại lệ trong chuỗi "main" java.lang.Unsa – Jinith

+0

1.- Nhấp chuột phải vào Dự án 2.- Thuộc tính 3.- Nhấp vào RUN 4.- Tùy chọn VM: java -Djava.library.path = "your_path" 5.- ví ​​dụ trong trường hợp của tôi: java -Djava.library.path = 6.- Ok –

20

Kể từ Java 1,7 bạn có thể sử dụng Watch Service API để đăng ký cho các sự kiện thư mục. Nó là một phần của thư viện New I/O (NIO) của Java và không yêu cầu bất kỳ tài nguyên bổ sung nào. Một ví dụ về cách sử dụng API có thể được tìm thấy trong số official documentation.

Sau khi đăng ký các WatchService bạn có thể lấy các sự kiện cho đường dẫn mục tiêu như thế này:

for (WatchEvent<?> event: key.pollEvents()) { 
      // Context for directory entry event is the file name of entry 
      WatchEvent<Path> ev = cast(event); 
      Path name = ev.context(); 
      Path child = dir.resolve(name); 

      // print out event 
      System.out.format("%s: %s\n", event.kind().name(), child); 
     } 
Các vấn đề liên quan