WatchService / detect 重命名和/或移动
注意:将 INSERT/DELETE 替换为 WatchService 中的相应事件...
还有一个关于 Java7 中的 WatchService 类的问题。如何可靠地检测目录/文件的重命名(甚至可能是移动)?更深入地思考,似乎很难检测到重命名,或者更准确地说,它涉及大量需要跟踪的状态。我认为检查相同文件/目录标识符的 INSERT/DELETE 或 DELETE/INSERT 对还不够。我认为仅跟踪一个事件是不够的。它也可能会干扰其他文件的删除或插入。因此,我能想到的只是一个非常丑陋的启发式方法,用于跟踪唯一标识符并监视插入和随后的删除,这可能会干扰其他事件或删除/插入。因此,我认为我需要某种超时,在超时后只需插入/删除所有跟踪的更改。绝对容易出错或者尽力检测重命名:-( 我认为可以从 Linux 和 Windows 获取相关信息(RENAME 事件),但我目前不打算使用 JNI,因为我已经实现了所有内容WatchService(即使我只花了几天)。
Note: Replace INSERT/DELETE with the appropriate events from WatchService...
One more question regarding the WatchService class from Java7. How can I reliably detect renames (and maybe even moves) of directories/files? On a more thorough thought it seems very hard to even detect renames, or more precisely it involes a lot of state to keep track of. I don't think it's enough to check for INSERT/DELETE or DELETE/INSERT pairs of the same file/directory identifier. I think it's not enough to keep track of only one event. It might as well interfere with other DELETEs or INSERTs of files. Thus all I can think of is a really ugly heuristic to keep track of unique identifiers and to watch for an INSERT and a subsequent DELETE which might interfere with other events or DELETE/INSERT. Thus I think I would need some kind of timeout and after it expires just insert/delete all tracked changes. Definitely error prone or a best effort to detect renames :-( I think it's possible to get the relevant info from Linux and Windows (RENAME event), but I currently don't intend to use JNI, as I've already implemented everything with WatchService (even if I've spend only a few days).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StandardWatchEventKinds.ENTRY_MODIFY 是您正在寻找的活动。
StandardWatchEventKinds.ENTRY_MODIFY is the event you are looking for.