本文介绍了Java 中的目录侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想在其中监听对特定目录所做的任何更改.一旦在该目录中添加、删除或更新了任何文件,应用程序应该立即 ping 我.

I have an application in which I want to listen to any changes made to a particular directory. The application should ping me as soon as there are any files added, deleted or updated in that directory.

推荐答案

你可以使用 JNotify

JNotify 是一个 java 库,允许 java 应用程序监听文件系统事件,例如:文件创建文件修改文件重命名文件删除了支持的平台

Windows(2000 或更新版本)Windows 注释支持 INofity 的 Linux(2.6.14 或更新的)Linux 注释 Mac OS X(10.5 或更新)Mac OS 注释

Windows (2000 or newer) Windows notes Linux with INofity support (2.6.14 or newer) Linux notes Mac OS X (10.5 or newer) Mac OS notes

更多信息:

此处下载 JNotify

解压zip,根据平台将.dll/.so放在你的lib路径中.并在类路径中创建一个类提供 jnotify-0.93.jar.

Extract the zip, put .dll/.so according to platform in your lib path. and create a class provide jnotify-0.93.jar in class path.

示例代码:

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 Settingsjigar: LOCALS~1Tempetilqs_4s8ywsvyukghK0uDxRop
modified C:Documents and Settingsjigar : LOCALS~1Tempetilqs_4s8ywsvyukghK0uDxRop
modified C:Documents and Settingsjigar : LOCALS~1Tempoutput1295531079119
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefault
deleted C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001ea9
created C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eae
modified C:Documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiom
modified C:Documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiom
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCache
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eae
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eae
modified C:Documents and Settingsjigar : LOCALS~1Tempoutput1295531079119
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCurrent Session
deleted C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001ea8
created C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eaf
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCache
modified C:Documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiom
modified C:Documents and Settingsjigar : LOCALS~1Tempetilqs_04gchL79ZJrpClZIqiom
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eaf
modified C:Documents and Settingsjigar : Local SettingsApplication DataGoogleChromeUser DataDefaultCachef_001eaf

这篇关于Java 中的目录侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!