如何找出为什么renameTo()失败? [英] How to find out why renameTo() failed?

查看:160
本文介绍了如何找出为什么renameTo()失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WinXP。我使用java来生成一个文件列表。该文件首先被创建为abc.txt.temp,完成生成后,它将被重命名为abc.txt。然而,当我生成文件,一些文件未能被重命名。它是随机发生的。



有没有办法找出失败的原因?

  int maxRetries = 60; 
logger.debug(retry);
while(maxRetries - > 0)
{
if(isSuccess = file.renameTo(file2))
{
break;

尝试
{
logger.debug(retry+ maxRetries);
Thread.sleep(1000);

catch(InterruptedException e)
{
// TODO自动生成的catch块
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,系统]
线程[附加Listener,5,系统]
java.lang.ThreadGroup [name = main,maxpri = 10]
线程[main,5,main]
线程isSuccess:false

[log4j mail appender,5,main]
[DEBUG] [2009-08-25 08:57:54,386]

我想知道一个系统的方法来找出原因。谢谢。

解决方案

重命名失败的原因可能是该文件仍处于打开状态。即使你正在关闭文件,也可能会因为(例如)而保持打开:


  1. 文件句柄由子进程继承您的进程

  2. 防病毒程序正在扫描文件中的病毒,并打开它。

  3. 索引器(例如Google桌面或Windows索引服务)已打开文件

为了帮助找出保留文件的工具,使用 FileMon

更新:一种工具,如Unlocker如果文件只保持打开很短的时间(如防病毒扫描的情况),则可能无济于事。但是,如果javaw.exe显示为打开该文件,那就是您的问题。


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.

解决方案

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):

  1. A file handle is inherited by a subprocess of your process
  2. An anti-virus program is scanning the file for viruses, and so has it open
  3. An indexer (such as Google Desktop or the Windows indexing service) has the file open

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()失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆