Java nio FileSystem Watcher锁定目录。删除变得不可能 [英] Java nio FileSystem Watcher locks directories. Deletion becomes impossible

查看:166
本文介绍了Java nio FileSystem Watcher锁定目录。删除变得不可能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java7的新功能来查看目录以进行更改(如以下教程中所述: http://download.oracle.com/javase/tutorial/essential/io/notification.html

I'm using the new feature of Java7 for watching directories for changes (as it is described in the following tutorial: http://download.oracle.com/javase/tutorial/essential/io/notification.html )

观看本身(几乎)没有问题。

The watching itself works (nearly) without problems.

或多或少我有两个问题,我希望你们中的一些人知道如何处理它。

More or less I have two issues and I hope some of you have an idea how to handle it.


  1. 添加一个文件夹进行监视后,VM可以访问它并且不会释放它。这应该不是一个大问题。但是,如果您正在另一个目录中创建一个目录,那么还有一个目录。 (例如,您在目录中创建 c:\ tmp \ a 目录 b 并在 b 目录 c c:\ tmp\a\b\c ))由于VM访问 b ,因此无法删除目录 a c 。如果要删除所有文件夹(例如,使用Windows资源管理器),则必须先删除 c ,然后 b 然后 a 。这很奇怪,但这种方式有效(虽然不方便)

  1. After a folder is added to watch, the VM has access to it and it doesn't release it. This should not be such a big issue. But if you're creating a directory in another directory and in this one more. (e.g. you create in directory c:\tmp\a the directory b and in b the directory c (c:\tmp\a\b\c)) it is not possible to delete the directory a, because of the access of the VM to b and c. If you want to delete all of the folders (e.g., with your Windows Explorer) you have to delete c first, then b and then a. It's strange, but this way works (though is inconvenient)

这个问题似乎是第一个问题的结果。
如果有时会(或多或少)发生 a 中的许多更改,我会得到一个异常,告诉我,新创建的文件夹被另一个进程使用而它不是可以访问它。这很奇怪,因为我认为收到通知无关紧要。并且因为完全抛出这个异常并不完全清楚。

This issue seems to be a result of the first one. If many changes in a occur sometimes (more or less) I get an exception which tells me, the newly created folder is used by another process and it is not possible to access it. It's strange, because I think this should not matter to get a notification. And because it is not completely clear when exactly this exception is thrown.

你知道如何制作它吗?可能没有锁或至少让用户能够以经典方便的方式删除这样的目录结构?

Do you have an idea how to make it possible to not have the lock or at least to let the user the ability to delete such a directory structure in the classical convenient way?

推荐答案

<嗯我已经尝试过在您发布的链接中展示的代码,执行您尝试执行的相同操作:

Well I've tried the code showcased in the link you posted, doing the same things you're trying to do:

在我的c:\ temp目录中我以编程方式创建ac:\ temp \ a dir然后ac:\ temp \\\\ b目录:

In my c:\temp directory I create programatically a c:\temp\a dir and then a c:\temp\a\b directory:

File startDir = new File("c:\temp");
        if(!startDir.exists()) {
            startDir.mkdir();
        }
        File aDir = new File("c:\\temp\\a");
        File bDir = new File("c:\\temp\\a\\b");
        if(!aDir.exists()) {
            aDir.mkdir();
        }
        if(!bDir.exists()) {
            bDir.mkdir();
        }

然后我将观察者添加到a和b目录:

Then I add watchers to the "a" and "b" directories:

public static void watch(final File dir,final WatchService watcher) {
        Path path = dir.toPath();
        try {
            final WatchKey bDirWatchKey = path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
            new Thread(new Runnable() {
                public void run() {
                    System.out.println("Watching: "+dir.getName());
                    while(true) {
                        try {Thread.sleep(1000);} catch (InterruptedException e) {}
                        List<WatchEvent<?>> events = bDirWatchKey.pollEvents();
                        for(WatchEvent<?> event:events) {
                            System.out.println(dir.getName()+" event: #"+event.count()+","+event.kind()+" File="+event.context());
                        }
                    }                   
                }
            }).start();
        } catch (IOException x) {
            x.printStackTrace();
        }
    }

如果我在a中修改文件,这项工作正常或b我得到相应的控制台输出。

This works ok, if I modify files in "a" or "b" I get the corresponding console output.

使用Windows资源管理器(在Windowx XP机器上)确实无法删除已监视的目录(它告诉我我没有访问权限)。但是我可以使用其他工具删除它,例如Total Commander。我甚至可以使用 rd c:\ temp \\\\ b 从Windows命令行中删除。我认为这对Windows资源管理器来说比使用Java更具问题......

It's true that with Windows Explorer (on a Windowx XP machine) I cannot delete a watched directory (it tells me I don't have access rights). I can however delete it with other tools such as Total Commander. I can even delete then from the Windows command line with rd c:\temp\a\b. I think this is more of an issue with Windows Explorer than with Java...

这篇关于Java nio FileSystem Watcher锁定目录。删除变得不可能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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