利用JDK7的NIO2.0进行I/O读写和文件操作监控最近在学习新的jdk 7提供的NIO 2.0,发现这个东东提供的java.nio.file包里的若干类,大大的方便了文件读写操作,而且编码相当简单,做了很好的封装。它的一个核心类就是Path。下面就是windows系统下新增,删除,拷贝,move文件的简单示例,注意,需要JDK7的编译和运行环境import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;import static java.nio.file.StandardWatchEventKinds.OVERFLOW;import java.io.IOException;import java.nio.file.FileSystems;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import static java.nio.file.LinkOption.NOFOLLOW_LINKS;import java.nio.file.WatchEvent;import java.nio.file.WatchKey;import java.nio.file.WatchService;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;public class MonitorDir {Map<WatchKey,Path> keys = new ConcurrentHashMap<WatchKey,Path>();private static WatchService watcher = null;static {try {watcher = FileSystems.getDefault().newWatchService();} catch (IOException e) {e.printStackTrace();}}private void register(Path dir) throws IOException { // IOException ,InterruptedException{WatchKey key = dir.register(watcher, ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY); Path existing = keys.get(key); if (existing == null) { System.out.format("register: %s\n", dir); } else if (!dir.equals(existing)){ System.out.format("update: %s -> %s\n",existing, dir); } keys.put(key, dir); }@SuppressWarnings("unchecked") static <T> WatchEvent<Path> cast(WatchEvent<?> event) { return (WatchEvent<Path>)event; } private void monitor(Path dir) throws IOException,InterruptedException{register(dir);// 等待监视事件发生WatchKey key = watcher.take();// System.out.println(key.getClass().getName());Path path = keys.get(key);if (path == null) {return;}for (WatchEvent<?> event : key.pollEvents()) {WatchEvent.Kind kind = event.kind();if (kind == OVERFLOW) {continue;}// 目录监视事件的上下文是文件名WatchEvent<Path> evt = cast(event);Path name = evt.context();Path child = path.resolve(name);System.out.format(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + " %s|%s\n", event.kind().name(), child);}// 重置 keyboolean valid = key.reset();if (!valid) {keys.remove(key);if (keys.isEmpty()) {return;}}}public static void main(String[] args) {MonitorDir monitorDir = new MonitorDir();Path dir = Paths.get("f:/monitortest");try {monitorDir.monitor(dir);} catch (IOException | InterruptedException e) {e.printStackTrace();}}}