2011-01-20 16 views
5

मैं एक बाहरी अनुप्रयोगएक अन्य आवेदन

मैं सिर्फ कोशिश कर रहा हूँ एक दिया पाठ फ़ाइल की सामग्री को संशोधित करता है जब अगर यह संभव थे पता करने के लिए, जावा में, एक ईवेंट सक्रिय करना चाहते हैं द्वारा पहुँचा फ़ाइल है एक छोटे से सॉफ्टवेयर है कि स्वचालित रूप से एक दूरस्थ एफ़टीपी सर्वर

उत्तर

7

करने के लिए एक स्थानीय फाइल सिंक्रनाइज़ करता है कार्यक्रम आप इस lib कोशिश कर सकते हैं JNotify

अधिक जानकारी:

डाउनलोड JNotify fr ओम here

ज़िप को निकालें, अपने lib पथ में प्लेटफॉर्म के अनुसार .dll/.so डाल दें। और वर्ग पथ में 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 
+0

कुछ जानकारी जोड़ा, बस इसे करने की कोशिश की, इस बारे में पता करने में खुशी। +1 –

+0

प्लेटफॉर्म निर्भर है लेकिन यह मुझे आवश्यक नौकरी करता है। धन्यवाद – William

0

नहीं, आप को तिथि फ़ाइल संशोधित किया गया था की जाँच करके एक निरंतर आधार पर अपने आप को इस पर नजर रखने की आवश्यकता होगी या सामग्री का हैश कर रहा है (चेकसम, आदि)। आम तौर पर, अंतर्निहित ओ/एस से कोई ईवेंट संचालित जावा-जेनेरिक फीडबैक नहीं होता है, इसलिए यदि कोई अतिरिक्त अतिरिक्त समर्थन है, तो यह ओ/एस निर्भर होगा।

जावा में कोई भी पुस्तकालय जो इसका समर्थन करता है, अनिवार्य रूप से इसे स्वयं ही कर रहा है।

0

आप प्रत्येक कुछ सेकंड में एक थ्रेड मतदान फ़ाइल कर सकते हैं और इसके संशोधन समय की जांच कर सकते हैं।

इसके अलावा, यह पता चला है कि जावा 7 WatchService API है अच्छा है:

WatchService एपीआई अनुप्रयोगों फ़ाइल परिवर्तन की घटनाओं के बारे में सूचित करने की आवश्यकता है के लिए बनाया गया है।

यहाँ यह पर एक पोस्ट है: Java 7: WatchService for File Change Notification

संबंधित मुद्दे