java中如何检测文件系统已更改

发布于 2024-08-25 10:00:47 字数 313 浏览 4 评论 0原文

我想知道如何在java中有效地实现文件系统更改?假设我在文件夹中有一个文件并修改该文件。我希望尽快得到 java 的有关此更改的通知(如果可能的话,不要频繁轮询。)。

因为我想我可以调用

alfred@alfred-laptop:~/testje$ java -version
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Server VM (build 16.0-b13, mixed mode)

I would like to know how to efficiently implement filesystem changes in java? Say I got a file in a folder and modify that file. I would like to be notified by java about this change as soon as possible(no frequently polling if possible.).

Because I think I could call java.io.file.lastModified every few seconds but I don't like the sound of that solution at all.

alfred@alfred-laptop:~/testje$ java -version
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Server VM (build 16.0-b13, mixed mode)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青瓷清茶倾城歌 2024-09-01 10:00:47

看一下JNotify,它执行这种类型的监视。

Java 7 将针对此类提供一些更高级的 API (WatchService)这将消除对支持此功能的操作系统的轮询。

Take a look at JNotify, which performs this type of monitoring.

Java 7 will have some more advanced APIs (WatchService) for this sort of work which will eliminate polling on the OSes that support this.

柠檬心 2024-09-01 10:00:47

我怀疑是否有一种纯java方法可以做到这一点。操作系统提供 API 来监视文件系统活动。您可能需要调用这些 API。

I doubt that there is a pure java way to do this. Operating systems offer APIs to monitor file system a activity. You will probably need to call those APIs.

感性 2024-09-01 10:00:47

使用JNotify,您需要做的就是在buildpath中添加jnotify.jar并放入两个dll文件,即jnotify.dll jnotify_64bit.dll和jdk的lib内部。演示程序是

package jnotify;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

public class MyJNotify {
  public void sample() throws Exception {
    String path = "Any Folder location here which you want to monitor";
    System.out.println(path);
    int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
            | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
    boolean watchSubtree = true;
    int watchID = JNotify
            .addWatch(path, mask, watchSubtree, new Listener());
    Thread.sleep(1000000);
    boolean res = JNotify.removeWatch(watchID);
    if (!res) {
        System.out.println("Invalid");
    }
}

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) {
    try {
        new MyJNotify().sample();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

Use JNotify, All you need to do is add jnotify.jar in buildpath and put two dll files i.e jnotify.dll jnotify_64bit.dll and inside lib of jdk. A demo program is

package jnotify;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;

public class MyJNotify {
  public void sample() throws Exception {
    String path = "Any Folder location here which you want to monitor";
    System.out.println(path);
    int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED
            | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
    boolean watchSubtree = true;
    int watchID = JNotify
            .addWatch(path, mask, watchSubtree, new Listener());
    Thread.sleep(1000000);
    boolean res = JNotify.removeWatch(watchID);
    if (!res) {
        System.out.println("Invalid");
    }
}

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) {
    try {
        new MyJNotify().sample();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}
勿挽旧人 2024-09-01 10:00:47

有一个 Apache 软件包可以进行文件系统监控:commons.io.monitor。

文档

示例

据我所知,尽管您可以控制频率,但您仍然需要进行轮询。

There is an Apache package that does file system monitoring: commons.io.monitor.

Documentation

An example

From what I can tell, you will still need to poll albeit you have control over the frequency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文