Java NIO watch Service created both 'ENTRY_CREATE' and 'ENTRY_MODIFY' when a new File is added to the watch folder

我们两清 提交于 2019-12-05 03:35:59

Check the "Platform dependencies" section in the WatchService JavaDoc.

The Watcher is behaving correctly in the situation you described. And strictly speaking, when you copy a file to a folder, the files really is CREATED and then MODIFIED.

There are many things to consider when using a WatcherService, its behavior varies a lot with for example:

  • Operating systems
  • Encrypted discs
  • Network shares
  • ...
Kuvaldis

I think, the question has already been answered here.

For instance Java: WatchService gets informed before content is copied

In two words, the events are generated by your operation system. You've got two events which means your OS performs copy in non-atomic way. If you try to read the file somewhere between ENTRY_CREATE and ENTRY_MODIFY, it'll be inconsistent. But again, it depends on your OS. For instance on Windows 7 I'm getting ENTRY_CREATE one time and ENTRY_MODIFY two times for large files. So you actually cannot be sure whether or not the file is successfully copied, you have to come up with application/OS dependent metrics.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!