您的位置:首页 > 编程语言 > Java开发

java 7 /java.nio.file 文件监控!!!

2013-03-29 19:00 330 查看
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="Z:\\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();
}
}

}

这个伤不起啊!!!不用了!

监测本地可用,但是监测碗盘不好用,



本来第一进入监视的话,first应该是空的,本地监控first是null,而监控贡献文件时会first不是null,还弄不明白它是在那初始化的!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: