Java 如何找出 renameTo() 失败的原因?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1325388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out why renameTo() failed?
提问by janetsmith
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.
我正在使用 WinXP。我使用java来生成文件列表。该文件首先创建为 abc.txt.temp,生成完成后将重命名为 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.
我想知道一种系统的方法来找出原因。谢谢。
采纳答案by Vinay Sajip
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):
重命名失败的原因可能是文件仍然打开。即使您正在关闭文件,它也可能因为(例如)而保持打开状态:
- A file handle is inherited by a subprocess of your process
- An anti-virus program is scanning the file for viruses, and so has it open
- An indexer (such as Google Desktop or the Windows indexing service) has the file open
- 文件句柄由进程的子进程继承
- 防病毒程序正在扫描文件中的病毒,因此已将其打开
- 索引器(例如 Google 桌面或 Windows 索引服务)打开了文件
To help find out what is keeping the file open, use tools such as FileMonand Handle.
要帮助找出保持文件打开的原因,请使用FileMon和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.
更新:如果文件只在很短的时间内保持打开状态(反病毒扫描就是这种情况),那么诸如 Unlocker 之类的工具可能无济于事。但是,如果 javaw.exe 显示为打开文件,那就是您的问题。
回答by Dave Ray
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.
如果没有抛出异常(我假设您会注意到),则renameTo()
仅返回 true 或 false 以指示重命名是否成功,并且不提供任何其他信息。
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.
由于它是 Windows,失败很可能表明该文件当前正在使用中。这会发生,因为其他一些进程打开了它。但更有可能的是,您的进程要么没有完成写入,要么在完成写入后忘记关闭文件。
It is also possible that you passed in an invalid path, or the gave a non-existent path to the File
constructor.
也有可能您传入了无效路径,或者为File
构造函数提供了不存在的路径。
renameTo()
will only throw exceptions if there is a security violation (SecurityException
) or if you pass in a null
for the file to rename.
renameTo()
仅当存在安全违规 ( SecurityException
) 或您传入null
要重命名的文件时才会抛出异常。
回答by prashanth
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..
我有一个类似的问题,但这是在 unix 上。
随机重命名失败。我重新启动了这个过程 3 到 4 次,终于成功了。
仅供参考,该文件是由同一进程创建的,并且同一进程将其重命名..
回答by ste
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..
文件 f=新文件(文件夹+文件);验证你是否写了正确的路径.. f.exists(); else 存在并返回 false 验证与 procMon 如果看起来..
回答by Sathish
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.
n.delete()
: 如果存在,我们需要删除文件(new.txt)。
o.rename(n)
: so that the file(old.txt) is renamed as new.txt
o.rename(n)
:这样文件(old.txt)被重命名为new.txt
回答by coolcool1994
Three Major reasons renameTo can fail (for Android, but you may also find this useful)!
renameTo 可能失败的三个主要原因(对于 Android,但您也可能会发现这很有用)!
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!
1) 如果您要将文件夹从 a 位置移动到 b 位置,则目标文件夹可能是一个文件!使 destinationFolder.mkdirs() 成为一个文件!
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
2)目标文件夹可能已经存在!删除目标文件夹,以便您可以使用 renameTo 将旧文件移动到新位置
3) Moving internal storage to external storage requires permission, because reading and writing to SD card requires permission!
3)将内部存储移动到外部存储需要许可,因为读写SD卡需要许可!
回答by Per Lindberg
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
也有可能您没有重命名文件,因为您没有足够的权限。在 Unix 上,这很简单。在 Win10 上,嗯……参见例如https://www.sevenforums.com/tutorials/1911-take-ownership-shortcut.html