public class WatchServiceWrapper extends SimpleWrapper<WatchService> implements WatchService, Serializable
WatchEvent
包装类,提供可选的监听事件和监听选项,实现方法包括:
registerPath(Path, int)
注册需要监听的路径。watch(Watcher, Predicate)
启动监听并指定事件触发后的行为。raw
Constructor and Description |
---|
WatchServiceWrapper(WatchEvent.Kind<?>... events)
构造
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
boolean |
isClosed()
是否已关闭
|
static WatchServiceWrapper |
of(WatchEvent.Kind<?>... events)
创建WatchServiceWrapper
|
WatchKey |
poll() |
WatchKey |
poll(long timeout,
TimeUnit unit) |
WatchKey |
register(Watchable watchable)
注册单一的监听
|
WatchServiceWrapper |
registerPath(Path path,
int maxDepth)
递归将指定路径加入到监听中
如果提供的是目录,则监听目录本身和目录下的目录和文件,深度取决于maxDepth |
WatchServiceWrapper |
setEvents(WatchEvent.Kind<?>... events)
监听事件列表,见:StandardWatchEventKinds
|
WatchServiceWrapper |
setEvents(WatchKind... kinds)
监听事件列表,见:StandardWatchEventKinds
|
WatchServiceWrapper |
setModifiers(WatchEvent.Modifier... modifiers)
设置监听选项,例如监听频率等,可设置项包括:
1、com.sun.nio.file.StandardWatchEventKinds
2、com.sun.nio.file.SensitivityWatchEventModifier
|
WatchKey |
take() |
void |
watch(BiConsumer<WatchEvent<?>,WatchKey> action)
执行事件获取并处理
WatchEvent.context() 是实际操作的文件或目录的相对监听路径的Path,非绝对路径WatchKey.watchable() 是监听的Path此方法调用后阻塞线程,直到触发监听事件,执行后退出,无循环执行操作 |
void |
watch(BiConsumer<WatchEvent<?>,WatchKey> action,
Predicate<WatchEvent<?>> watchFilter)
执行事件获取并处理
WatchEvent.context() 是实际操作的文件或目录的相对监听路径的Path,非绝对路径WatchKey.watchable() 是监听的Path此方法调用后阻塞线程,直到触发监听事件,执行后退出,无循环执行操作 |
void |
watch(Watcher watcher,
Predicate<WatchEvent<?>> watchFilter)
执行事件获取并处理
WatchEvent.context() 是实际操作的文件或目录的相对监听路径的Path,非绝对路径WatchKey.watchable() 是监听的Path此方法调用后阻塞线程,直到触发监听事件,执行后退出,无循环执行操作 |
getRaw
public WatchServiceWrapper(WatchEvent.Kind<?>... events)
events
- 监听事件列表public static WatchServiceWrapper of(WatchEvent.Kind<?>... events)
events
- 监听事件列表,如新建、修改、删除等public boolean isClosed()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
close
in interface WatchService
public WatchKey poll()
poll
in interface WatchService
public WatchKey poll(long timeout, TimeUnit unit) throws InterruptedException
poll
in interface WatchService
InterruptedException
public WatchKey take() throws InterruptedException
take
in interface WatchService
InterruptedException
public WatchServiceWrapper setEvents(WatchKind... kinds)
kinds
- 事件列表public WatchServiceWrapper setEvents(WatchEvent.Kind<?>... events)
events
- 事件列表public WatchServiceWrapper setModifiers(WatchEvent.Modifier... modifiers)
1、com.sun.nio.file.StandardWatchEventKinds 2、com.sun.nio.file.SensitivityWatchEventModifier
modifiers
- 监听选项,例如监听频率等public WatchKey register(Watchable watchable)
watchable
- 可注册对象,如PathWatchKey
,如果为null
,表示注册失败Watchable.register(WatchService, WatchEvent.Kind[])
,
Watchable.register(WatchService, WatchEvent.Kind[], WatchEvent.Modifier...)
public WatchServiceWrapper registerPath(Path path, int maxDepth)
path
- 路径maxDepth
- 递归下层目录的最大深度public void watch(Watcher watcher, Predicate<WatchEvent<?>> watchFilter)
WatchEvent.context()
是实际操作的文件或目录的相对监听路径的Path,非绝对路径WatchKey.watchable()
是监听的Pathwatcher
- Watcher
watchFilter
- 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,Predicate.test(Object)
为true
保留,null表示不过滤public void watch(BiConsumer<WatchEvent<?>,WatchKey> action)
WatchEvent.context()
是实际操作的文件或目录的相对监听路径的Path,非绝对路径WatchKey.watchable()
是监听的Pathaction
- 监听回调函数,实现此函数接口用于处理WatchEvent事件public void watch(BiConsumer<WatchEvent<?>,WatchKey> action, Predicate<WatchEvent<?>> watchFilter)
WatchEvent.context()
是实际操作的文件或目录的相对监听路径的Path,非绝对路径WatchKey.watchable()
是监听的Pathaction
- 监听回调函数,实现此函数接口用于处理WatchEvent事件watchFilter
- 监听过滤接口,通过实现此接口过滤掉不需要监听的情况,Predicate.test(Object)
为true
保留,null表示不过滤Copyright © 2025. All rights reserved.