forked from JavaDevTeam/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-file-WatchService.java
More file actions
153 lines (136 loc) · 3.98 KB
/
Copy pathjava-file-WatchService.java
File metadata and controls
153 lines (136 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-----------------------
文件监视 |
-----------------------
# 监视某个目录下文件的
* 创建
* 删除
* 修改
# 涉及类库
WatchService
WatchKey
WatchEvent<?>
WatchEvent.Kind
# 事件
StandardWatchEventKinds.ENTRY_CREATE 文件创建
StandardWatchEventKinds.ENTRY_DELETE 文件删除
StandardWatchEventKinds.ENTRY_MODIFY 文件修改
StandardWatchEventKinds.OVERFLOW 事件丢失,一般不关注
-----------------------
WatchService |
-----------------------
# 接口方法
@Override
void close() throws IOException;
WatchKey poll();
WatchKey poll(long timeout, TimeUnit unit) throws InterruptedException;
WatchKey take() throws InterruptedException;
-----------------------
WatchKey |
-----------------------
# 接口方法
boolean isValid();
List<WatchEvent<?>> pollEvents();
boolean reset();
void cancel();
Watchable watchable();
-----------------------
WatchEvent |
-----------------------
# 接口方法
Kind<T> kind();
int count();
T context();
# 内部接口
public static interface Kind<T> {
String name();
Class<T> type();
}
public static interface Modifier {
String name();
}
-----------------------
Demo |
-----------------------
//WatchService 是线程安全的,跟踪文件事件的服务,一般是用独立线程启动跟踪
public static void watchRNDir(Path path) throws Exception {
//创建 WatchService 对象
WatchService watchService = FileSystems.getDefault().newWatchService();
//给path路径加上文件观察服务
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
// 开始监视路径
while (true) {
//线程阻塞
final WatchKey key = watchService.take();
//获取事件集合
List<WatchEvent<?>> watchEventList = key.pollEvents();
//遍历
for (WatchEvent<?> watchEvent : watchEventList) {
// 获取事件
final WatchEvent.Kind<?> kind = watchEvent.kind();
// handle OVERFLOW event
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
//创建事件
if (kind == StandardWatchEventKinds.ENTRY_CREATE) {
}
//修改事件
if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
}
//删除事件
if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
}
//把当前事件强制转换泛型为 Path 的事件
final WatchEvent<Path> watchEventPath = (WatchEvent<Path>) watchEvent;
//获取事件文件名称
final Path filename = watchEventPath.context();
// print it out
System.out.println(kind + " -> " + filename);
}
// reset the keyf
boolean valid = key.reset();
// exit loop if the key is not valid (if the directory was
// deleted, for
if (!valid) {
break;
}
}
}
------------------------------------
WatchService |
------------------------------------
# 构建
WatchService watchService = FileSystems.getDefault().newWatchService();
# 方法
WatchKey take();
* 检索并移除下一个watch key。若没有可检索的则阻塞。
------------------------------------
WatchKey |
------------------------------------
# 构建
通过 WatchService 实例的 take()方法获取
# 方法
List<WatchEvent<?>> pollEvents();
* 检索并移除所有该watch key
------------------------------------
WatchEvent |
------------------------------------
# 构建
# 方法
Kind<?> kind();
* 返回事件种类
------------------------------------
Kind |
------------------------------------
# 方法
String name();
* 返回事件的名称
------------------------------------
StandardWatchEventKinds |
------------------------------------
# 事件类
# 静态字段
OVERFLOW
ENTRY_CREATE
ENTRY_DELETE
ENTRY_MODIFY