2011-02-09 11 views
17

मेरे पास एक ऐसा एप्लिकेशन है जिसमें मैं किसी विशेष निर्देशिका में किए गए किसी भी बदलाव को सुनना चाहता हूं। जैसे ही कोई भी फाइल जोड़ा गया है, हटाया गया है या उस निर्देशिका में अपडेट किया गया है, एप्लिकेशन को मुझे पिंग करना चाहिए।जावा में निर्देशिका श्रोता

उत्तर

21

आप JNotify

JNotify उपयोग कर सकते हैं एक जावा पुस्तकालय है कि जावा आवेदन जैसे प्रणाली घटनाओं, फ़ाइल को सुनने के लिए अनुमति देते हैं: फ़ाइल बनाई फ़ाइल संशोधित फ़ाइल पुनर्नामित फ़ाइल समर्थित हटाए गए प्लेटफार्मों

विंडोज (2000 या नया) विंडोज INofity समर्थन (2.6.14 या नवीन) लिनक्स नोट्स मैक ओएस एक्स के साथ लिनक्स नोट्स (1 से here

डाउनलोड JNotify ज़िप निकालें, डाल .dll/.so अपने lib रास्ते में मंच के अनुसार: 0.5 या नवीन) मैक ओएस नोट

अधिक जानकारी। और वर्ग पथ में jnotify-0.93.jar प्रदान करते हैं।

नमूना कोड:

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(); 
    } 
} 

आउटपुट:

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

* डाउनवॉटर * कृपया –

+1

पर टिप्पणी करें जब मैं प्रोग्राम चलाता हूं तो यह मुझे अपवाद देता है त्रुटि लोडिंग लाइब्रेरी, java.library.path = C : \ प्रोग्राम फ़ाइलें \ जावा \ jdk1.7.0 \ bin;। सी: \ विंडोज \ सूर्य \ जावा \ bin; सी: \ विंडोज \ system32; सी: \ विंडोज; सी: \ विंडोज; सी: \ विंडोज \ system32; सी: \ विंडोज \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; सी: \ प्रोग्राम फ़ाइलें \ Microsoft SQL सर्वर \ 90 \ उपकरण \ binn \; सी: \ apache-tomcat-6.0.26 \ bin; सी: \ कार्यक्रम फ़ाइलें \ जावा \ jdk1.7।0 \ bin; सी: \ प्रोग्राम फ़ाइलें \ TortoiseSVN \ bin; सी: \ प्रोग्राम फ़ाइलें \ पुटी; सी: \ प्रोग्राम फ़ाइलें \ Google \ क्रोम \ अनुप्रयोग; सी: \ प्रोग्राम फ़ाइलें \ जावा \ jdk1.7.0 \ थ्रेड में अपवाद शामिल करें "मुख्य" java.lang.Unsa – Jinith

+0

'ज़िप को निकालें, अपने lib path में प्लेटफॉर्म के अनुसार .dll/.so डाल दें।' –

3

जावा में फ़ाइल अधिसूचना के लिए jnotify। कोड का नमूना

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

जब मैं कार्यक्रम चलाने यह मेरे exceptionError लाइब्रेरी लोड करने, java.library.path = सी देता है \ jdk1.7.0 \ बिन; सी:। \ Windows \ सूर्य \ जावा \ बिन; C: \ Windows \ system32; C: \ Windows; C: \ Windows; C: \ Windows \ system32; C: \ Windows \ system32 \ Wbem; \ WindowsPowerShell \ v1.0 \; सी: \ प्रोग्राम फ़ाइलें \ Microsoft SQL Server \ 90 \ उपकरण \ binn \; सी: \ apache-tomcat-6.0.26 \ bin; सी: \ प्रोग्राम फ़ाइलें \ जावा \ jdk1 .7.0 \ bin; सी: \ प्रोग्राम फ़ाइलें \ TortoiseSVN \ bin; सी: \ प्रोग्राम फ़ाइलें \ पुटी; सी: \ प्रोग्राम फ़ाइलें \ Google \ क्रोम \ अनुप्रयोग; सी: \ प्रोग्राम फ़ाइलें \ जावा \ jdk1.7.0 \ अपवाद शामिल करें थ्रेड में "मुख्य" java.lang.Unsa – Jinith

+0

1.- प्रोजेक्ट पर राइट क्लिक करें 2.- गुण 3.- रन पर क्लिक करें 4.- वीएम विकल्प: जावा-डीजावा.library.path = "your_path" 5.- उदाहरण के लिए मेरे मामले में: जावा -Djava.library.path = 6.- ठीक –

20

जावा 1.7 के बाद से आप निर्देशिका की घटनाओं के लिए रजिस्टर करने के लिए Watch Service API उपयोग कर सकते हैं। यह जावा की New I/O (एनआईओ) लाइब्रेरी का हिस्सा है और किसी भी अतिरिक्त संसाधन की आवश्यकता नहीं है। एपीआई का उपयोग करने का एक उदाहरण official documentation में पाया जा सकता है।

WatchService पंजीकृत करने के बाद आप इस तरह लक्ष्य पथ के लिए घटनाओं को पुनः प्राप्त कर सकते हैं: \ Program Files \ जावा:

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); 
     } 
संबंधित मुद्दे