检测目录是否在java中被修改

我有一个要求, 我需要validation我的Java应用程序创build的目录的修改。

用户可以通过创build文件和删除文件来修改目录或修改,所以我只想validation特定目录是否被修改。

我的解决scheme:我需要一个包含最后修改时间的散列,每次我必须通过我的散列匹配最后修改时间的目录。

但我的技术人员反对说有很多技巧和库,以便用户可以修改其最后修改时间。

所以告诉我,请replace它。

使用java 7 watch service api

 for (;;) { WatchKey key; try { key = watcher.take(); } catch (InterruptedException x) { return; } for (WatchEvent<?> event: key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); if (kind == OVERFLOW) { continue; } WatchEvent<Path> ev = (WatchEvent<Path>)event; Path filename = ev.context(); try { Path child = dir.resolve(filename); if (!Files.probeContentType(child).equals("text/plain")) { System.err.format("New file '%s'" + " is not a plain text file.%n", filename); continue; } } catch (IOException x) { System.err.println(x); continue; } System.out.format("Emailing file %s%n", filename); } boolean valid = key.reset(); if (!valid) { break; } }