Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

File.renameTo() and locking

843829Dec 1 2005 — edited Dec 2 2005
Hi,

I would like to rename file, but File.renameTo() will not work if the file will be locked by some other process. If other process has locked the file, I would like to wait for it to release the lock. So I have:
FileLock lock = new RandomAccessFile(file, "rw").getChannel().lock(0L, Long.MAX_VALUE, false);
file.renameTo(destFile);
lock.release();
But this does not work, because file is locked and renameTo does not work (returns false).

What can I do to lock the file myself (wait for other locks to be released) and to rename it (rename does not work when file is locked).

Regards
Pawel Stawicki
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 30 2005
Added on Dec 1 2005
5 comments
308 views