package chpter15.test.ao1;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.text.SimpleDateFormat;
import java.util.Date;
public class WatchServiceTest {
public static void main(String[] args) throws IOException, InterruptedException {
WatchService watchService = FileSystems.getDefault().newWatchService();
Paths.get("c:\\").register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.OVERFLOW);
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while(true) {
WatchKey key = watchService.take();
for(WatchEvent<?> event:key.pollEvents()) {
System.out.println(bartDateFormat.format(new Date())+"\t"+"c:\\"+event.context()+"\t"+event.kind()+"\t");
Thread.sleep(1);
}
boolean valid = key.reset();
if(!valid) {
break;
}
}
}
}
java NIO文件系统监控
最新推荐文章于 2021-07-24 11:33:22 发布