I'd like to know when a source file has been completely copied to its destination. The portable solution, of course, is to poll the file based on modified time or size. In Windows, this doesn't work when copying files within the same file system, in my case, the file system is NTFS, and the file�s complete size is noted as soon as the first byte is written. Unlike most *nixes the modified time doesn�t change upon each byte being written. Instead, the modified time is the same as the source file�s.
Using the standard API, how can one tell when a file has been completely written?
File.length(), File.lastModified() obviously wont work.
File.canRead() (and File.canWrite()) will return true. Yet, when I try to open the file (which is still in progress), FileInputStream() throws a FileNotFoundException, stating that �The process cannot access the file because it is being used by another process�.
This seems to be a bug in File.canRead() since the javadoc says:
public boolean canRead()
Tests whether the application can read the file denoted by this abstract pathname.
true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise.
Using Java 1.6 build 105
So, using the standard API, how is one supposed to tell if a file is still being copied to its dest? Surely one wouldn�t have to do something as ugly as force the Exception and check the message.
My take on the API description of canRead() - the javadocs for 1.6 of both the method and the class - is that it checks access permissions. Other things can still go wrong at runtime making the file unreadable.