Detecting file completion using WatchService in java
I have a file which is being constantly written to, while it is registered with watchService. Is it possible to detect that it has completed, using WatchEvent.
Please note that, by complete, I mean the previous writer which was writing to the file has finished and I expect no-one else to open that file and write.
I have faced the same problem on Windows paltform. I have tried to solve it using timeout: when a timeout of 1 second for example is elapsed (since the last modify event), I consider that file is complete. But it is not working for big files: I have remark that a modify event is fired after the timeout. In my application, I copy files on a directory. I solved the problem by copying a temporary file after the copy of my file. In this case, when a create event is fired for the temporary file, the first file is complete.
N.B: for linux platform, I've done some searching and I find a suggestion to use renameTo() method to check if the file still locked by a process. If the remaneTo() returns true, your file is complete. In my project, which is Windows dependant, I tried renameTo() but I get a true value and the file is not complete.