packagecom.supermap;importjava.io.File;importjava.io.IOException;importjava.nio.file.FileSystems;importjava.nio.file.Path;importjava.nio.file.StandardWatchEventKinds;impo...
package com.supermap;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(FileSystems.getDefault());
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
String watchDir="E:\\test";
File watchDirFile = new File(watchDir);
System.out.println(watchDirFile.isDirectory());
Path watchDirPath = watchDirFile.toPath();
WatchKey watchKey = watchDirPath.register(watchService,StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_MODIFY);
try {
while(true){
watchKey=watchService.take();
System.out.println("==========");
for (WatchEvent event : watchKey.pollEvents()) {
System.out.println("An event was found after file creation of kind" + event.kind()+". The event occurred on file" + event.context());
}
watchKey.reset();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
此代码监测本地文件可以,监测共享文件,任何操作多不触发事件!!求大神解决,源码已经看不进去了!
妹的,怎么缩进没了,大神忍忍吧!随让“知道”也有不知道的啊!
展开