我可以通过WatchService监视单个文件的更改(不是整个目录)吗?

当我尝试注册文件而不是目录时java.nio.file.NotDirectoryException。我可以听一个文件更改,而不听整个目录吗?



侃侃尔雅
浏览 650回答 3
3回答

偶然的你

只需过滤目录中所需文件的事件即可:final Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"), "Desktop");System.out.println(path);try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {&nbsp; &nbsp; final WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);&nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; final WatchKey wk = watchService.take();&nbsp; &nbsp; &nbsp; &nbsp; for (WatchEvent<?> event : wk.pollEvents()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //we only register "ENTRY_MODIFY" so the context is always a Path.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Path changed = (Path) event.context();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(changed);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (changed.endsWith("myFile.txt")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("My file has changed");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // reset the key&nbsp; &nbsp; &nbsp; &nbsp; boolean valid = wk.reset();&nbsp; &nbsp; &nbsp; &nbsp; if (!valid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Key has been unregisterede");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在这里,我们检查更改后的文件是否为“ myFile.txt”,如果已更改则执行任何操作。

蝴蝶刀刀

其他答案是正确的,您必须监视目录并筛选特定文件。但是,您可能希望线程在后台运行。接受的答案可以无限期地阻止,watchService.take();并且不会关闭WatchService。适用于单独线程的解决方案可能如下所示:public class FileWatcher extends Thread {&nbsp; &nbsp; private final File file;&nbsp; &nbsp; private AtomicBoolean stop = new AtomicBoolean(false);&nbsp; &nbsp; public FileWatcher(File file) {&nbsp; &nbsp; &nbsp; &nbsp; this.file = file;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean isStopped() { return stop.get(); }&nbsp; &nbsp; public void stopThread() { stop.set(true); }&nbsp; &nbsp; public void doOnChange() {&nbsp; &nbsp; &nbsp; &nbsp; // Do whatever action you want here&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; try (WatchService watcher = FileSystems.getDefault().newWatchService()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path path = file.toPath().getParent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (!isStopped()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WatchKey key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try { key = watcher.poll(25, TimeUnit.MILLISECONDS); }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (InterruptedException e) { return; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (key == null) { Thread.yield(); continue; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (WatchEvent<?> event : key.pollEvents()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WatchEvent.Kind<?> kind = event.kind();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @SuppressWarnings("unchecked")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WatchEvent<Path> ev = (WatchEvent<Path>) event;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path filename = ev.context();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (kind == StandardWatchEventKinds.OVERFLOW) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.yield();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (kind == java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && filename.toString().equals(file.getName())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doOnChange();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean valid = key.reset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!valid) { break; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.yield();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (Throwable e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Log or rethrow the error&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我尝试从公认的答案和本文进行工作。您应该能够与此线程一起使用new FileWatcher(new File("/home/me/myfile")).start()并通过调用该线程将其停止stopThread()。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java