发个例子给大家看下吧- public class Test {
- public static void main(String[] args) {
- WatchService watcher = null;
- try {
- watcher = FileSystems.getDefault().newWatchService();
- Path dir = FileSystems.getDefault().getPath("E:\\");
- WatchKey key = dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE,
- StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
- while (true) {
- key = watcher.take();
- for (WatchEvent<?> event : key.pollEvents()) {
- System.out.println(event.kind().name());
- System.out.println(event.context());
- System.out.println("00-------------------00");
- }
- key.reset();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
复制代码 |
|