Tag: watchservice

如何查看新内容的文件并检索该内容

我有一个名为foo.txt的文件。 该文件包含一些文本。 我想实现以下function: 我启动程序 写一些文件(例如添加一行: new string in foo.txt ) 我想获得此文件的新内容。 你能澄清这个问题的最佳解决方案吗? 此外,我想解决相关问题:如果我修改foo.txt我想看到差异。 我在Java中找到的最接近的工具是WatchService但如果我理解正确,这个工具只能检测文件系统上发生的事件类型(创建文件或删除或修改)。

释放Java 7 WatchService的资源

我正在使用Java 7 WatchService来查看目录。 我不断改变我正在观看的目录。 我遇到了exception: java.io.IOException:已达到网络BIOS命令限制。 在50个目录之后。 我确定我在创建一个新的WatchService之前调用close()。 有没有人知道释放WatchService的正确方法,所以你没有遇到这个限制? 谢谢, 戴夫

为什么WatchService会产生如此多的操作?

import java.io.*; import java.nio.file.*; public class Tmp { public static void main(String [] args) throws IOException { int count = 0; Path path = Paths.get(“C:\\tmp\\”); WatchService ws = null; try { ws = FileSystems.getDefault().newWatchService(); path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW); } catch (IOException ioe) { ioe.printStackTrace(); } while(true) { WatchKey key = null; try { key […]

如何查看文件夹和子文件夹以进行更改

我正在尝试查看特定文件夹以进行更改,然后如果在其中发生任何添加/编辑/删除,我需要获取该文件夹及其子文件夹中所有文件的更改类型。 我正在使用WatchService ,但它只观看单个路径,它不处理子文件夹。 这是我的方法: try { WatchService watchService = pathToWatch.getFileSystem().newWatchService(); pathToWatch.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); // loop forever to watch directory while (true) { WatchKey watchKey; watchKey = watchService.take(); // This call is blocking until events are present // Create the list of path files ArrayList filesLog = new ArrayList(); if(pathToWatch.toFile().exists()) { File fList[] = pathToWatch.toFile().listFiles(); […]

捕获目录内发生的事件

我正在使用Java 7 nio WatchService通过使用以下方法观看目录。 Path myDir = Paths.get(“/rootDir”); try { WatchService watcher = myDir.getFileSystem().newWatchService(); myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); WatchKey watckKey = watcher.take(); List<WatchEvent> events = watckKey.pollEvents(); for (WatchEvent event : events) { if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) { System.out.println(“Created: ” + event.context().toString()); JOptionPane.showMessageDialog(null,”Created: ” + event.context().toString()); } if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) { System.out.println(“Delete: ” + event.context().toString()); […]

如何使用WatchService查看子目录以进行更改? (JAVA)

我想看一些目录进行更改及其子目录。 我尝试使用WatchService执行此操作,但我无法知道文件更改的目录。 如何从WatchEvent检索完整路径?

在写入文件之前触发了Java 7 Watch Service ENTRY_CREATE

我有一个观看目录的观看服务。 创建文件后,我正在处理目录并更新树视图。 这在ENTRY_DELETE上工作正常,但有时(并非总是)当发生WatchEvent的ENTRY_CREATE时,该文件尚未写入磁盘。 我已经通过创建一个new File()来监视监视服务注册的目录以及文件的路径并检查exists()方法,所以似乎操作系统在文件之前触发了创建事件实际上是创造了。 这个问题似乎是同一个问题,但从文件夹的角度来看。 我可以用任何方式解决这个问题吗?

使用Java监视服务监视子文件夹

我正在使用watchKey来监听特定文件夹中的文件更改。 Path _directotyToWatch = Paths.get(“E:/Raja”); WatchService watcherSvc = FileSystems.getDefault().newWatchService(); WatchKey watchKey = _directotyToWatch.register(watcherSvc, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); while (true) { watchKey=watcherSvc.take(); for (WatchEvent event: watchKey.pollEvents()) { WatchEvent watchEvent = castEvent(event); System.out.println(event.kind().name().toString() + ” ” + _directotyToWatch.resolve(watchEvent.context())); watchKey.reset(); } } 它对我来说很好。 如果我修改raja文件夹中的文件,它会给我带路径的文件名。 但是,当我将一些文件放在像“E:/ Raja / Test”这样的子文件夹中时,它只给出了我放置它的路径,而不是文件名。 如何获取文件名?

Java WatchService在查看映射驱动器时不生成事件

我实现了一个文件监视器,但我注意到java nio文件观察器不会为映射驱动器上复制的文件生成事件。 例如,我在Unix上运行文件观察器来观察映射到Windows( H:\ )的本地目录( /sharedfolder ),然后我将一个文件放在这个目录中( H:\ )但是文件观察者没有生成任何事件。 现在,如果我在Windows上运行文件观察器来监视映射驱动器( H:\ ),它引用了unix路径( /sharedfolder ),并且从unix我将文件放在此文件夹中,文件监视器识别更改并生成事件。 它看起来像一个bug,或者可能是我错过了一些东西,任何想法?

WatchService和SwingWorker:如何正确地做到这一点?

WatchService听起来像是一个令人兴奋的想法…不幸的是,它似乎与教程/ api中的警告一样低,并不真正适合Swing事件模型(或者我错过了一些明显的,非零概率 从教程中的 WatchDir 示例中获取代码(只是为了处理单个目录),我基本上结束了 扩展SwingWorker 在构造函数中执行注册工作 把无限循环放在doInBackground中等待一个键 通过key.pollEvents()检索时发布每个WatchEvent 通过使用已删除/创建的文件作为newValue触发propertyChangeEvents来处理块 @SuppressWarnings(“unchecked”) public class FileWorker extends SwingWorker<Void, WatchEvent> { public static final String DELETED = “deletedFile”; public static final String CREATED = “createdFile”; private Path directory; private WatchService watcher; public FileWorker(File file) throws IOException { directory = file.toPath(); watcher = FileSystems.getDefault().newWatchService(); directory.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); } […]