Reputation: 19235
How can I get the JDK to perform an atomic move operation, but throw FileAlreadyExistsException
if the target already exist ?
My source and target are of type Path
. I'm guaranteed that they both reside in the same folder (have same parent) thus they are always on the same filesystem. In effect, what I'm looking for is a rename operation and I want to make certain (as certain as can be) that the OS does not start a copy operation. My files a fairly big (1-2 GB) and copying would really be stupid because they are in the same folder. And it would block for long time.
Example I've tried:
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
but this will overwrite any existing file. (which is indeed also what the Javadoc says)
My scenario is a rather busy one, meaning quite a few processes going at the filesystem at the same time. Therefore, checking first if the file exist is not really the best solution as I risk getting an existing file - from another thread/process - overwritten. I can also do with a solution with opportunistic "ATOMIC_MOVE" but the Javadoc is vague on if this is possible. (in my case it would be good enough because opportunistic would mean always in my case)
(no, not the same question as this one)
Upvotes: 1
Views: 162