获取修改文件中的变化事件? [英] Getting to modify events on changes in file?

查看:173
本文介绍了获取修改文件中的变化事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nio.2类的Watch Service API来观察特定的目录(Ubuntu 11.10)。无论何时做出更改,例如,如果我在现有文件中进行修改,则会发生2次修改事件。不知道为什么?创建新文件时工作正常。

解决方案

我找到了解决方案。它触发了两个修改事件,因为当文件系统写入新文件时,它会用零字节创建它,并触发创建事件,然后将数据写入和文件,然后触发修改事件。

这同样适用于修改。当我们修改一个已经存在的文件时,文件系统首先用0字节创建它,然后触发一个修改事件,然后写入数据。然后它再次触发修改事件。这就是为什么我收到两个修改事件。

I am using Watch Service API of nio.2 classes to watch a specific directory( Ubuntu 11.10). whenever a change is made, for eg if I modify in existing file, it fires a MODIFIED event 2 times. Don't know why? it's work fine when I create new file.

解决方案

I found a solution for it. It fires two modify events, because when the filesystem writes new files, it creates it with zero byte and fires create event and then it writes data to the and file then it fires the modify event.

The same applies with modifications. When we modify an already existing file, the filesystem first creates it with 0 bytes and fires a modify event and then writes data to it. Then it fires the modify event again. That's why I was receiving two modify events.

这篇关于获取修改文件中的变化事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆