Java7的新IO,NIO2

WatchService监听文件变化

 1 package chapter3;
 2 
 3 import java.io.IOException;
 4 import java.nio.file.FileSystems;
 5 import java.nio.file.Paths;
 6 import java.nio.file.StandardWatchEventKinds;
 7 import java.nio.file.WatchEvent;
 8 import java.nio.file.WatchKey;
 9 import java.nio.file.WatchService;
10 import java.util.List;
11 
12 public class WatchServiceTest {
13 
14     public static void main(String[] args) throws IOException, InterruptedException {
15         WatchService ws = FileSystems.getDefault().newWatchService();
16         Paths.get("C:/").register(ws, StandardWatchEventKinds.ENTRY_CREATE);
17         while (true) {
18             WatchKey wk = ws.take();
19             List<WatchEvent<?>> weList =  wk.pollEvents();
20             for (WatchEvent<?> we : weList) {
21                 System.out.println(we.context() + " 文件发生了: " + we.kind() + "事件!");
22             }
23             boolean valid = wk.reset();
24             if (!valid) {
25                 break;
26             }
27         }
28     }
29 
30 }
View Code

 

转载于:https://www.cnblogs.com/djjwwz/p/5215252.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值