Java7 WatchService - 尝试删除递归监视的嵌套目录时出现“拒绝访问”错误(仅限Windows) [英] Java7 WatchService - Access Denied error trying to delete recursively watched nested directories (Windows only)

查看:499
本文介绍了Java7 WatchService - 尝试删除递归监视的嵌套目录时出现“拒绝访问”错误(仅限Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照观看更改目录 Java7 nio2使用代码示例 WatchDir以递归方式监视目录的全部内容的教程.java

I followed the Watching a Directory for Changes Java7 nio2 tutorial to recursively monitor the entire contents of a directory using the code sample WatchDir.java.

虽然这在Linux和Mac上运行良好,但在Windows上(在Vista和7上测试过),尝试使用Windows删除嵌套的,已观看的文件夹当一个文件存在于其中一个嵌套目录中时,资源管理器失败并显示类似于拒绝访问:您需要执行此操作的权限的消息。

While this works well on Linux and Mac, on Windows (tested on Vista and 7), trying to delete nested, watched folders using Windows Explorer fails with a message akin to "Access Denied: You need permission to perform this action" when a file exists in one of the nested directories.

例如,如果我在Windows中观看嵌套文件夹树:

For example, if I watch a nested folder tree in Windows:

-- Folder A
   -- Folder A1
      -- File F

并尝试删除文件夹A,我得到上述拒绝访问错误。但是,如果我:它可以正常工作:

and try to delete Folder A, I get the said Access Denied error. However, it works fine if I:


  • 删除文件夹A1然后删除文件夹A

  • 删除文件F然后删除文件夹A

有没有办法使用nio2 WatchService递归观察嵌套目录,但不能表现得像程序正在访问嵌套文件?

Is there a way to use the nio2 WatchService to recursively watch a nested directory, but not act as though a program is accessing nested files?

推荐答案

如果您正在Windows上查看目录,那么WatchService实现具有该目录的打开句柄(这就是Windows的工作方式)。该打开句柄不会阻止删除该目录,但它会阻止立即删除该目录的父目录。删除监视目录后,句柄将关闭,但您可能会在句柄关闭之前尝试删除目录。当发生这种情况时,您将获得拒绝的访问权限。如果你重试,我认为它适用于你,这是因为你重试时手柄将被关闭。

If you are watching a directory on Windows then the WatchService implementation has an open handle to that directory (that's the way that Windows works). That open handle doesn't prevent the directory from being deleted but it does prevent the directory's parent from being deleted immediately. As soon as you delete the watched directory then the handle is closed but it's possible that you will attempt to delete the directory before the handle is closed. When that happens you will get the access denied that you are seeing. I assume it works fine for you if you retry and this is because the handle will be closed by the time you retry.

Windows上的Sun JRE可以使用Windows手表如果在注册调用中指定 ExtendedWatchEventModifier.FILE_TREE 修饰符,则可以帮助绕过此问题只有一个文件句柄。

The Sun JRE on Windows can use Windows' watch subtree capability if you specify the ExtendedWatchEventModifier.FILE_TREE modifier in the register call, which can help bypass this issue as it creates only one file handle.

这篇关于Java7 WatchService - 尝试删除递归监视的嵌套目录时出现“拒绝访问”错误(仅限Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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