简体   繁体   English

Java WatchService 删除父目录时不报告文件

[英]Java WatchService does not report files when the parent directory is deleted

I have the following files and folders structure:我有以下文件和文件夹结构:

/root/

/root/pictures/

/root/pictures/picture1.jpg

/root/pictures/picture2.jpg

I registered two WatchServices, one for the /root/ folder and one for /root/pictures .我注册了两个 WatchService,一个用于/root/文件夹,一个用于/root/pictures For both I registered the events: ENTRY_CREATE , ENTRY_DELETE , ENTRY_MODIFY .对于我注册的事件: ENTRY_CREATEENTRY_DELETEENTRY_MODIFY

When I delete /root/pictures/ I expect to get one ENTRY_DELETE event for the deletion of the folder /root/pictures/ and two ENTRY_DELETE events for picture1.jpg and picture2.jpg .当我删除/root/pictures/我希望得到一个ENTRY_DELETE事件文件夹的删除/root/pictures/两个ENTRY_DELETE事件picture1.jpgpicture2.jpg In fact I only get the ENTRY_DELETE event for /root/pictures/ .事实上,我只得到/root/pictures/ENTRY_DELETE事件。 When I only delete picture1.jpg I get one delete event as expected.当我只删除picture1.jpg我会按预期得到一个删除事件。

Is that normal behaviour?这是正常行为吗? How can I get the list of files that were inside a deleted folder with WatchService?如何使用 WatchService 获取已删除文件夹中的文件列表?

You did not show any code, so we don't know how you tried to implement it, but the following seems to work to get ENTRY_DELETE events for files within a directory if the directory itself is deleted (please note that it contains only one WatchService, for directory xxx/yyy; I have not included the other WatchService for directory xxx)您没有显示任何代码,所以我们不知道您是如何尝试实现它的,但是如果目录本身被删除,以下内容似乎可以为目录中的文件获取 ENTRY_DELETE 事件(请注意,它只包含一个 WatchService , 对于目录 xxx/yyy;我没有包含目录 xxx 的其他 WatchService)

public class WatchServiceApp {

    public static void main(String[] args) throws IOException {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        Path dirPath = Paths.get("/home/myuser/xxx/yyy");
        WatchKey watchKey = dirPath.register(
                watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

        while (true) {
            try {
                watchService.take();
            } catch (InterruptedException ex) {
                break;
            }

            List<WatchEvent<?>> watchEventList = watchKey.pollEvents();
            for (WatchEvent<?> watchEvent : watchEventList) {
                Path filePath = (Path) watchEvent.context();
                System.out.println("Event " + watchEvent.kind() + " for " + filePath.toString());
            }

            boolean watchKeyValid = watchKey.reset();
            if (!watchKeyValid) {
                break;
            }
        }
    }

}

Lets assume the directory xxx/yyy contains three files.假设目录 xxx/yyy 包含三个文件。 First we delete individual file3 and get首先我们删除单个文件3并得到

Event ENTRY_DELETE for file3

then we delete the entire yyy directory and get然后我们删除整个 yyy 目录并得到

Event ENTRY_DELETE for file2
Event ENTRY_DELETE for file1

It looks like you does not deleted your directory finally.看起来您最终没有删除您的目录。 I mean, if you delete directory in Windows by key "delete", you will got an event for the deletion of the folder /root/pictures/.我的意思是,如果您通过“删除”键删除 Windows 中的目录,您将收到一个删除文件夹 /root/pictures/ 的事件。 But your files will still alive in the trash can.但是您的文件将仍然存在于垃圾桶中。 If you will clear trash can - you will get ENTRY_DELETE events for picture1.jpg and picture2.jpg如果您将清除垃圾桶 - 您将获得 picture1.jpg 和 picture2.jpg 的 ENTRY_DELETE 事件

From the documentation on WatchService (emphasis mine):来自WatchService的文档(重点是我的):

Platform dependencies平台依赖

The implementation that observes events from the file system is intended to map directly on to the native file event notification facility where available , or to use a primitive mechanism, such as polling, when a native facility is not available.从文件系统观察事件的实现旨在直接映射到可用的本地文件事件通知工具,或者在本地工具不可用时使用原始机制,例如轮询。 Consequently, many of the details on how events are detected, their timeliness, and whether their ordering is preserved are highly implementation specific.因此,关于如何检测事件、它们的及时性以及它们的排序是否保留的许多细节都是高度特定于实现的。 [...] [...]

Meaning that much of the way events are delivered is platform dependent.这意味着事件传递的大部分方式都依赖于平台。 To your question, is that normal behaviour?对于您的问题,这是正常行为吗? The answer is: depends on the platform.答案是:取决于平台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM