如何使用 JDK 7 区分文件删除和目录删除?
我正在使用 JDK 7 的 WatchService 来监视目录。
ENTRY_DELETE
事件告诉我一个条目已被删除。我可以通过执行类似以下操作来获取该条目的名称:
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
我想知道删除的条目是文件还是文件夹。当然,我尝试了 child.isDirectory()
但这当然不起作用,因为该元素不再存在。
有没有什么方法,无需启发式,就能判断删除的元素是文件还是目录?
I am using JDK 7's WatchService to monitor directories.
The ENTRY_DELETE
event tells me an entry has been deleted. I can get the name of that entry doing something similar to:
WatchEvent<Path> ev = cast(event);
Path name = ev.context();
Path child = dir.resolve(name);
I want to know if the deleted entry was a file or folder. Naturally, I tried child.isDirectory()
but that didn't work, of course, because the element doesn't exist any more.
Is there any way, without heuristics, telling if the deleted element was a file or a directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,由于 WatchService 构建在本机操作系统的文件事件服务之上,因此它受到本机服务提供的信息的限制。 Linux 的 inotify 确实指示删除了哪种类型的文件系统对象,但 Microsoft 的 FileSystemWatcher 只是给出名称。
Unfortunately, since WatchService is built on top of the native OS's file event service, it is limited by the information the native service provides. Linux's inotify does indicate what type of filesystem object was deleted, but Microsoft's FileSystemWatcher just gives the name.