问题标签 [watchservice]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
java - 获取有关 Java 中更改的文件的信息
我正在尝试监视java中的文件。当文件修改发生时,我想知道:
- 哪个过程发生了变化。
- 发生了什么变化。
另外,我知道有一种方法可以更改文件中的“最后修改日期”,所以我想检查是否有人更改了该字段。
我尝试了“commons.io”、“DefaultFileMonitor”和“WatchService”,但我能从它们那里得到的所有信息都是是否发生了更改以及文件是否已更改。
java - java watchservice中实现重命名和删除
我试图在 java WatchService 中实现简单的重命名。
我的假设是:文件重命名时,会执行三个操作
- 删除文件 xxx
- 创建文件 yyy
- 修改文件 yyy
以下是我的代码:
MyWatcher.java
文件重命名.java
它在重命名操作上完美显示,但问题是我无法弄清楚如何显示 onDelete。因为每次删除都会推送到通知中!!或者帮我实现重命名!!
*注意请不要推荐第三方罐子!(因为它们中的大多数,如 JNotify,都依赖于操作系统)
java - 使用 WatchService 进行单元测试代码
下面是一个使用 WatchService 保持数据与文件同步的简短示例。我的问题是如何可靠地测试代码。测试偶尔会失败,可能是因为 os/jvm 将事件送入监视服务和轮询监视服务的测试线程之间存在竞争条件。我的愿望是保持代码简单、单线程和非阻塞,但同时也是可测试的。我非常不喜欢将任意长度的睡眠调用放入测试代码中。我希望有更好的解决方案。
而目前的测试
java - WatchService: missed and unhandled events
I'm having an issue with WatchService. Here is a snippet of my code:
*First of all, know that watch() is called inside an infinite loop.
The problem is that when creating multiple files at a time, some events are missing. For example, if I copy-paste three files into the ".../request" folder, only one gets caught, the others remain as if nothing happened, neither an OVERFLOW event is triggered. In some different Computer and OS, it reaches up to two files, but if one tries 3 or more, the rest still untouched.
I found a workaround though, but I don't think it's the best practice. This is the flow:
The process starts and then stops at
as expected, (as per Processing events). Then, I drop 3 files together in "request" folder, thus, process resumes at
The issue is here. It seems like the thread goes so fast through this line that two CREATED events stay behind and are lost, only one is taken. The workaround was to add an extra line right above this one, like this:
This seems to be a solution, at least for three and several more simultaneous files, but it's not scalable at all. So in conclusion, I would like to know if there is a better solution for this issue. FYI, I'm running a Win 7 64
Thanks a lot in advance!
java - 如何在应用程序关闭时终止 WatchService?
我有一个为以下代码WatchService
抛出一个:ClosedWatchServiceException
如何在不出现异常的情况下安全地关闭服务?或者我应该忽略关闭,因为在终止应用程序时无论如何都会杀死任何线程?
java - UNIX sys/classes/gpio 文件的 NIO watchservice
是否可以在/sys/class/gpio/gpioX
设备树中的文件上设置 JAVA NIO WatchService 以监督那里的更改?
直接从 Java 中检测 GPIO 文件(即 GPIO 输入)的更改是个好主意,但我担心这不受支持。
确认它不受支持(也许是为什么)就足够了。
java - 为什么 Path 类中的 register 方法返回 WatchKey?(观看服务 API,Java SE 7)
观看服务 api ( https://docs.oracle.com/javase/tutorial/essential/io/notification.html ) 看起来有点复杂和违反直觉。所以我很困惑如何正确使用它。让我们按照上面网址中的指南进行操作。
register 方法返回 WatchKey 对象。在指南中它通常被省略。它很奇怪。怎么能用?它从寄存器返回的原因是什么?因为获取 WatchKey 的正常方式是这样的:
java - WatchService:注册时传递参数
我正在寻找一种在将路径注册到 WatchService 时传递参数的方法。目标是在处理与路径相关的事件时获取这些参数。
知道是否可能吗?
谢谢,米凯尔
java - 文件的 Java 监视器文件夹
我需要监视某个文件夹以查找需要处理的新文件。我有以下要求:
- 文件的文件名是序列号。我需要按顺序处理每个文件。(最小的数字在前,不保证每个序列号都存在。例如:1,2,5,8,9
- 如果启动时文件夹中已经存在文件,我需要直接处理
- 我需要保证每个文件只处理一次
- 我需要避免读取不完整的文件(仍在复制中)
- 服务当然应该是可靠的...
实现这一目标的最常见方法是什么?
我正在使用 Java SE7 和 Spring 4。
我已经看过 Java 7 的 WatchService,但它似乎在启动期间处理已经存在的文件有问题,并避免处理不完整的文件。
java - 使用 WatchService 识别 .txt 中的更改
我正在尝试使用 WatchService 扫描目录以查找对多个文件的任何更改。我能够识别给定目录中已更改的确切文本文件。我想知道是否有某种方法可以让我的程序准确显示更改是什么,然后在更改中搜索特定变量?
新条目看起来像这样
2015 年 7 月 2 日 11:44, 88, 55
我想拉这条线,然后扫描 88 或 55 是否达到 100 或更高。
这是我当前的代码。