java 7 directory monitoring questions

萝らか妹 提交于 2019-12-04 04:07:42

It looks like you're talking about the WatchService.

The wording of the ENTRY_CREATE event says that a new entry would be perceived if a new file is created or a file is renamed into the directory. It lacks specification of what events are fired if a file is renamed and remains in the same directory.

The wording also states that whether the service is based on the operating system or polling is implementation dependent. I suspect that's implementation by the JRE, so even if you know a particular OS supports it, it's not a guarantee that the service will use the OS-level functionality or resort to polling. In fact, the service does not provide any way to tell whether it is using polling or an OS-level feature at all.

The operations the API define also do not behave like a Listener. The WatchService does do automatic watching, but to acquire the list of events which happen, you still have to manually request the seen events from the service. It doesn't appear to provide any hooks to automatically get called when a new event is present.

If you play with it on Windows and on Linux, you'll see lots of differences in behavior. So Java does not really attempt to provide a consistent platform independent abstraction. You have to test your app on the OSes you care about (well, there are only 2).

WatchService sucks more than you can imagine. Prepare for frustrations if you really dive into it.

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