2011-01-20 41 views
5

Tôi muốn biết nếu có thể, trong java, để kích hoạt sự kiện khi ứng dụng bên ngoài sửa đổi nội dung của tệp văn bản đã choTệp có được truy cập bởi một ứng dụng khác

Tôi đang cố gắng chương trình một phần mềm nhỏ mà tự động đồng bộ hóa một tập tin địa phương đến một máy chủ fTP từ xa

Trả lời

7

Bạn có thể thử lib này JNotify

More Info:

Tải JNotify fr om here

Giải nén zip, put .dll/.so theo nền tảng trong đường dẫn 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 
+0

thêm vài thông tin, chỉ cần thử nó, vui mừng khi biết về việc này. +1 –

+0

Phụ thuộc vào khuôn dạng nhưng nó thực hiện công việc tôi cần. Cảm ơn – William

0

Không, bạn sẽ cần phải theo dõi này cho mình trên cơ sở liên tục, hoặc bằng cách kiểm tra ngày tệp được sửa đổi hoặc làm một hash của nội dung (checksum, vv). Nói chung, không có phản hồi Java-chung hướng sự kiện từ O/S cơ bản, vì vậy nếu có bất kỳ hỗ trợ bổ sung nào, nó sẽ phụ thuộc vào O/S.

Bất kỳ thư viện nào trong Java hỗ trợ tính năng này chủ yếu làm việc này.

0

Bạn có thể có một cuộc thăm dò chủ đề của tệp mỗi vài giây và kiểm tra thời gian sửa đổi của nó.

Ngoài ra, nó là tốt để biết rằng Java 7 có WatchService API:

Các WatchService API được thiết kế cho các ứng dụng cần phải được thông báo về các sự kiện thay đổi tập tin.

Đây là một bài đăng trên nó: Java 7: WatchService for File Change Notification

Các vấn đề liên quan