在Java中查看文件和目录更改 [英] Watching for file and directory changes in Java

查看:147
本文介绍了在Java中查看文件和目录更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里写的主要是关于从哪里开始的建议。我已经实现了一个使用Java的WatchService以递归方式查看目录的类。它可以很好地检测更改,但我注意到一个致命的缺陷:我无法删除正在监视包含正在监视的目录的目录。这似乎是WatchService的一个限制。

I'm writing here mostly for advice on where to start. I've already implemented a class that will recursively watch a directory using Java's WatchService. It works quite alright for detecting changes, but I've noticed a fatal flaw: I cannot delete directories that are being watched that contain directories that are being watched. This seems to be a limitation of WatchService.

我已经研究过Apache的VFS FileListener了一下,但是在我花费大约6个小时的时间建设之前围绕着它的某种包装,我想也许我只会问那些比我自己更有知识的人。

I have also looked into Apache's VFS FileListener a bit, but before I spend another 6-or-so hours of my time building some kind of wrapper around it, I figured maybe I'd just ask those more knowledgeable than myself.

我需要看到的目录是完全可操作的,但被监视的根目录不会被删除或重命名。是否已经有一个好的类我可以用来监视不锁定文件或文件夹的文件和目录?我试图避免使用轮询/哈希比较方法,但我感觉越来越多,好像我需要使用这种方法并烧掉大量的CPU资源。我从哪里开始呢?理想情况下,我需要:

I need the directories being watched to be fully manipulable, with the exception that the root directory being watched will not be deleted or renamed. Does there already exist a good class I can work with to monitor files and directories that does not lock files or folders? I'm trying to avoid a polling/hash comparison approach, but I feel more and more as if I will need to use that approach and burn through a ton of CPU resources. Where do I start with this? Ideally, I need to:

- 检测文件和目录的创建
- 检测文件和目录的删除
- 检测文件和目录的重命名
-Detect修改文件
-检测目录之间文件的移动

-Detect creation of files and directories -Detect deletion of files and directories -Detect renaming of files and directories -Detect modification of files -Detect movement of files between directories

我也看到一些人认为观察者不可靠并且他们使用这两者的组合(偶尔会在观察者失败的情况下进行轮询),但男人听起来像是一种真正的痛苦,如果不是最好的方式,我宁愿避免。我有一种感觉,我需要轮询和散列,特别是因为我想检测文件的移动和重命名,但请确定是否存在更好的选项。

I've also seen some suggest that watchers are unreliable and that they use a combination of the two (polling occasionally in case the watcher failed somewhere), but man that sounds like a real pain that I'd rather avoid if it isn't the best way anyways. I have a feeling I'll need polling and hashing, especially since I want to detect moving and renaming of files, but please do tell if better options exist.

谢谢提前,抱歉代码不一致的问题!

Thanks in advance, and sorry for the not-so-code-specific question!

推荐答案

这将允许您尝试创建,删除,移动和重命名D:\ Temp下的文件,并应该让你了解你需要的东西:

This will allow you to experiment with creating, deleting, moving and renaming files under D:\Temp, and should allow you to learn what you need:

import static com.sun.nio.file.ExtendedWatchEventModifier.FILE_TREE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class Foo3
{
    public static void main(String[] args) throws Exception
    {
        FileSystem fs = FileSystems.getDefault();
        WatchService ws = fs.newWatchService();
        Path pTemp = Paths.get("D:/Temp");
        pTemp.register(ws, new WatchEvent.Kind[] {ENTRY_MODIFY, ENTRY_CREATE, ENTRY_DELETE}, FILE_TREE);
        while(true)
        {
            WatchKey k = ws.take();
            for (WatchEvent<?> e : k.pollEvents())
            {
                Object c = e.context();
                System.out.printf("%s %d %s\n", e.kind(), e.count(), c);
            }
            k.reset();
        }
    }
}

这篇关于在Java中查看文件和目录更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆