如何从 Java watchservice 注销目录? [英] How do I unregister a directory from Java watchservice?

查看:80
本文介绍了如何从 Java watchservice 注销目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 watchService 中注册了一个文件夹:

I registered a folder to my watchService:

path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

稍后,我想取消此注册.我知道我需要告诉 watchService 我想取消哪个 WatchKey.完成此操作的正确功能是什么?

Later on, I want to cancel this registration. I know that I somehow need to tell the watchService which WatchKey I want to cancel. What's the correct function to accomplish this?

推荐答案

您在 Watchable 接口 javadoc 中有信息,该接口提供了注册 Watchable 对象的方法(例如一个 Path 实例)

You have the information in the Watchable interface javadoc that provides the method to register a Watchable object (such as a Path instance)

公共界面可观看

该接口定义了注册对象的注册方法一个 WatchService 返回一个 WatchKey 来表示注册.一个对象可以注册多个监视服务.通过调用密钥的注册取消取消方法.

This interface defines the register method to register the object with a WatchService returning a WatchKey to represent the registration. An object may be registered with more than one watch service. Registration with a watch service is cancelled by invoking the key's cancel method.

<小时>

所以你只需要做:


So you have just to do :

WatchKey watchKey = path.register(watchService, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
...
watchKey.cancel();

这篇关于如何从 Java watchservice 注销目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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