在 Windows 上测试。观察文件夹中的变化a
。当我将监视文件夹重命名a
为b
WatchService
静止轨道更改时。问题是那a
已经不存在了。(Path)event.context()
返回监视文件夹的相对路径。因此,您现在正在观看a
文件夹和文件abc.txt
已更改。但实际上它是放在文件夹中的b
。
如何获得更改文件的正确绝对路径?
在 Windows 上测试。观察文件夹中的变化a
。当我将监视文件夹重命名a
为b
WatchService
静止轨道更改时。问题是那a
已经不存在了。(Path)event.context()
返回监视文件夹的相对路径。因此,您现在正在观看a
文件夹和文件abc.txt
已更改。但实际上它是放在文件夹中的b
。
如何获得更改文件的正确绝对路径?
I have confirmed your issue on Windows 7. After changing the (watched) dir name, events are reported such that the associated file name is resolved with the original dir name. This is also the case when using jpathwatch: http://jpathwatch.wordpress.com/ or https://sourceforge.net/projects/jpathwatch/
Typically you are watching a "known" or "expected" directory such as "/myDropBox", that is not expected to change names. It would seem that if you expect that a watched dir might change its name then you would also need to register its parent directory. You would then see a "delete" event for the original name and "create" for the new name.