Reputation: 8732
I am using WinXP. I use java to generate a list of files. The file will be created as abc.txt.temp at first, and after completing the generation, it will be renamed to abc.txt.
However, when i generating the files, some of the files failed to be renamed. It happen randomly.
Is there anyway to find out the reason why it failed?
int maxRetries = 60;
logger.debug("retry");
while (maxRetries-- > 0)
{
if (isSuccess = file.renameTo(file2))
{
break;
}
try
{
logger.debug("retry " + maxRetries);
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//file.renameTo(file2);
Thread.currentThread().getThreadGroup().getParent().list();
And the result:
[DEBUG][2009-08-25 08:57:52,386] - retry 1
[DEBUG][2009-08-25 08:57:53,386] - retry 0
java.lang.ThreadGroup[name=system,maxpri=10]
Thread[Reference Handler,10,system]
Thread[Finalizer,8,system]
Thread[Signal Dispatcher,9,system]
Thread[Attach Listener,5,system]
java.lang.ThreadGroup[name=main,maxpri=10]
Thread[main,5,main]
Thread[log4j mail appender,5,main]
[DEBUG][2009-08-25 08:57:54,386] - isSuccess:false
I would like to know a systematic approach to figure out the reason. Thanks.
Upvotes: 32
Views: 63685
Reputation: 1
No one metioned this, and this just happened to me, so I figure'd I'd post it here:
The rename can fail if the name you're trying to rename it to is an invalid filename. For example, it contains colon characters on Windows.
A code example:
final File originalFile = new File(dbType.filename);
final String nowString = Instant.now().toString().replace(":", "_");
final File bakFile = new File(originalFile.getName() + "-" + nowString + ".db.bak");
the above will fail to rename originalFile to bakFile if you remove the .replace(":", "_");
Upvotes: 0
Reputation: 1
I'm seeing the same thing on Mac. I have a process that creates 163,000 files in a single thread. It skips creating any file that already exists. To avoid a partial file problem, when it comes time to write the file, it writes a temp file (.../dir/tmp.filename) and then renames it (.../dir/filename).
I ran it once (from inside IntelliJ), and then the run window disappeared, which was odd. I restarted IntelliJ and ran it again, and started getting errors on a bunch (but not all) of my file renames. It turned out that my previously-running java process was still running, even though IntelliJ (which had launched it) had quit. So I had two processes looking for the existence of the same files and stepping on each others' toes.
Upvotes: 0
Reputation: 757
It is also possible that you may not rename the file because you don't have sufficient permissions. On Unix, that's simple. On Win10, well... see e.g. https://www.sevenforums.com/tutorials/1911-take-ownership-shortcut.html
Upvotes: 0
Reputation: 3812
Three Major reasons renameTo can fail (for Android, but you may also find this useful)!
1) If you are moving folders from place a to place b, the destination folder may be a file! Make the destinationFolder.mkdirs() to make it a file!
2) The destination folder may already exist! Delete the destinationFolder so that you can use renameTo to move the old file to that new location
3) Moving internal storage to external storage requires permission, because reading and writing to SD card requires permission!
Upvotes: 2
Reputation: 31
File o=new File("d:/old.txt");
File n=new File("d:/new.txt");
n.delete();
o.renameTo(n);
n.delete()
: We need to delete the file(new.txt) if exists.
o.rename(n)
: so that the file(old.txt) is renamed as new.txt
Upvotes: 3
Reputation: 1
File f=new File(folder+file); verify with if you have write correct path.. f.exists(); else is exist and return false verify with procMon if is looked..
Upvotes: -4
Reputation: 21
I had a similar issue, but this is with unix.
The rename randomly failed. I restarted the process 3 to 4 times and finally went to success.
FYI the file was created by the same process and the same process renames it..
Upvotes: 2
Reputation: 99530
It's possible that the reason that renaming failed is that the file is still open. Even if you are closing the file, it could be held open because of (for example):
To help find out what is keeping the file open, use tools such as FileMon and Handle.
Update: A tool such as Unlocker may not help, if the file is only held open for a very short time (as would be the case for an anti-virus scan). However, if javaw.exe is shown as having the file open, that's your problem right there.
Upvotes: 34
Reputation: 40005
If no exceptions were thrown (I'm assuming you would have noticed that) renameTo()
only returns true or false to indicate whether the rename succeeded or not and doesn't give any additional information.
Since it's Windows, a failure most likely indicates the the file is currently in use. This would happen because some other process has it open. More likely though, your process either isn't finished writing it or you forgot to close the file after you were done writing it.
It is also possible that you passed in an invalid path, or the gave a non-existent path to the File
constructor.
renameTo()
will only throw exceptions if there is a security violation (SecurityException
) or if you pass in a null
for the file to rename.
Upvotes: 3