Javaの断片(C)上のいくつかのポイント

逐語https://www.dazhuanlan.com/2019/08/25/5d625ec2c8207/


直接コードに

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
パスパス= Paths.get(「パス」)。もし(Files.exists(パス)){ 試みる(WatchService watchService = FileSystems.getDefault()newWatchService()){                 path.register(                        watchService、                        StandardWatchEventKinds.ENTRY_CREATE、                        StandardWatchEventKinds.ENTRY_MODIFY、                        StandardWatchEventKinds.ENTRY_DELETE                 )。一方、){                     WatchKeyキー= watchService.take()。  //阻塞(WatchEventイベント<?>:key.pollEvents()){











WatchEvent <パス> EV =(WatchEvent <パス>)イベント;
パスファイル= ev.context(); //取得したイベントのドキュメント

もし(StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())){ //创建时 }



もし(StandardWatchEventKinds.ENTRY_MODIFY.equals(event.kind())){ //修改时 }



IF(StandardWatchEventKinds.ENTRY_DELETE.equals(event.kind())){ //削除 } } key.reset(); クリア治療の成功、またはフォローアップイベントがトリガすることはできませんした後に、キューキー// } } キャッチ(IOExceptionを| E InterruptedExceptionある){ e.printStackTrace(); }








いくつかの注意事項

  • あれば登録は、単一の文書の監視をサポートしていませんpathではない文档夹スローjava.nio.file.NotDirectoryException

おすすめ

転載: www.cnblogs.com/petewell/p/11408897.html