问题
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