文件是否被其他应用程序访问?

5

我想知道在Java中是否有可能,当外部应用程序修改给定文本文件的内容时触发事件。

我只是尝试编写一个小软件,自动将本地文件与远程FTP服务器同步。

3个回答

7
您可以尝试使用这个库JNotify
更多信息:
这里下载JNotify。
解压缩zip文件,根据平台将.dll/.so文件放在您的lib路径下,并在类路径中创建一个提供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

平台依赖性强,但它能够完成我需要的工作。谢谢。 - William

0
不,你需要持续自己监控这个,可以通过检查文件修改日期或对内容进行哈希(校验和等)来实现。一般来说,底层操作系统没有事件驱动的Java通用反馈,因此如果有任何可能的额外支持,它将依赖于操作系统。
任何在Java中支持此功能的库都是在自行处理。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接