如何找出 renameTo() 失败的原因?
我用的是WinXP。我使用java来生成文件列表。该文件最初会创建为abc.txt.temp,生成完成后将重命名为abc.txt。
但是,当我生成文件时,某些文件无法重命名。它是随机发生的。
有没有办法找出失败的原因?
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();
结果是:
[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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
重命名失败的原因可能是文件仍处于打开状态。即使您要关闭该文件,它也可能因以下原因而保持打开状态(例如):
要帮助找出使文件保持打开状态的原因,请使用诸如 FileMon 和 句柄< /a>.
更新:如果文件仅保持打开状态很短的时间(如防病毒扫描的情况),Unlocker 等工具可能无济于事。但是,如果 javaw.exe 显示为已打开文件,那就是您的问题了。
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.
如果没有抛出异常(我假设您已经注意到了),
renameTo()
仅返回 true 或 false 来指示重命名是否成功,并且不提供任何其他信息。由于它是 Windows,因此故障很可能表明该文件当前正在使用。发生这种情况是因为其他进程打开了它。但更有可能的是,您的进程尚未完成写入,或者您在写入完成后忘记关闭文件。
您也可能传入了无效路径,或者为
File
构造函数提供了不存在的路径。仅当存在安全违规 (
SecurityException
) 或您为要重命名的文件传入null
时,renameTo()
才会引发异常。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 anull
for the file to rename.n.delete()
:我们需要删除文件(new.txt)(如果存在)。o.rename(n)
:将文件(old.txt)重命名为new.txtn.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我也遇到了类似的问题,但这是在 unix 上。
重命名随机失败。我重新启动了3到4次,终于成功了。
仅供参考,该文件是由同一进程创建的,并且同一进程将其重命名。
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..
renameTo 失败的三个主要原因(对于 Android,但您可能也会发现这很有用)!
1)如果您将文件夹从a处移动到b处,目标文件夹可能是一个文件!使destinationFolder.mkdirs() 使其成为一个文件!
2) 目标文件夹可能已经存在!删除destinationFolder,以便您可以使用renameTo将旧文件移动到新位置
3)将内部存储移动到外部存储需要权限,因为读写SD卡需要权限!
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!
您也可能因为没有足够的权限而无法重命名该文件。在 Unix 上,这很简单。在 Win10 上,嗯...参见例如 https://www.sevenforums .com/tutorials/1911-take-ownership-shortcut.html
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
我在 Mac 上也看到同样的情况。我有一个进程在单个线程中创建 163,000 个文件。它会跳过创建任何已存在的文件。为了避免部分文件问题,在写入文件时,它会写入一个临时文件 (.../dir/tmp.filename),然后重命名它 (.../dir/filename)。
我运行了一次(从 IntelliJ 内部),然后运行窗口消失了,这很奇怪。我重新启动 IntelliJ 并再次运行它,并开始在一堆(但不是全部)文件重命名中出现错误。事实证明,即使 IntelliJ(启动它)已经退出,我之前运行的 java 进程仍在运行。所以我有两个进程寻找相同文件的存在并互相踩对方的脚趾。
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.
文件f=新文件(文件夹+文件);
验证您是否写了正确的路径..
f.exists();
else 存在并返回 false 如果查看则使用 procMon 进行验证..
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..