views:

1788

answers:

2

Hi,

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.

A: 

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.

Dave Ray
janetsmith
+1  A: 

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.

Vinay Sajip
I am using Unlocker. and it only shows "javaw.exe" as the only locker. :(
janetsmith
+1 for point 2, 3
janetsmith
I would think that scanning applications like anti-virus and indexers shouldn't be able to hold the file and prevent it from being altered or renamed... Is this actually the case in WinXP?
Yuval
They don't hold the file open for long, but typically watch for file changes and scan after updates to the file. If you write to a file, close it and then try to rename it, the file *could* still be held open at that point (though not for long).
Vinay Sajip