I have a watch service watching a directory. Once files are created, I'm processing the directory and updating a tree view.
This works fine on ENTRY_DELETE
, but sometimes (not always) when a WatchEvent
of ENTRY_CREATE
occurs, the file has not yet been written to the disk.
I've confirmed this by creating a new File()
of the directory the watch service is registered to along with the path of the file and checking the exists()
method, so it seems that the OS is triggering the create event before the file is actually created.
This question appears to be the same issue, but from the folder's point of view.
Any way I can work around this?
The event is triggered when a file is created. The file needs to be created before it can be written to. A file doesn't simply appear once it is fully written, it appears once it is created.
What you can do is once you get the creation event:
rw
mode (so read & write access)A simplified example:
File lockFile = new File( "file_to_lock" );
FileChannel channel = new RandomAccessFile( lockFile, "rw" ).getChannel( );
channel.lock( );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With