JAVA NIO Watcher: How to detect end of a long lasting (copy) operation?

瘦欲@ 提交于 2019-12-02 04:29:02

问题


I need to make some ZIP- action on a freshly introduced file in a dir. There I subsribe for the CREATE event, which is fired. The copy operation into that directory takes some time.

So I get ACCESS_DENIED, "locked by another process" when accessing the file.

Does NIO provide something like "LOCK Released" or do I need to poll the file somehow for the lock to be released ? Like described here :http://stackoverflow.com/questions/750471/how-to-know-whether-a-file-copying-is-in-progress-complete-in-java-1-6

Thanks for any help.

Gerd


回答1:


copy the file in a different directory; after it's done, move it to the watched directory.

java.nio.file.Files.move(srcFile, targetFile, StandardCopyOption.ATOMIC_MOVE);

the watcher will see one CREATE event




回答2:


Modify the copy operation so that an additional, small file is written to the same directory, after the first, larger file is written. The filename could be the name of the original file, plus an extension.

Your watcher would look for files with this extension, chop it off to get the original filename, and then start the Zip-action.

Another option could be that your copy operation writes a file with an extension other than the one you look for. When done, it renames the file to give it the correct extension.



来源:https://stackoverflow.com/questions/14386608/java-nio-watcher-how-to-detect-end-of-a-long-lasting-copy-operation

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