cannot rename or delete File instances
I'm writing a Maven plugin that deletes and renames various files using the File.delete()
a开发者_如何转开发nd File.renameTo(File)
JDK methods.
Roughly every second time I run the plugin, one of these operations fails, and each time it fails it's a different file that cannot be deleted or renamed. An obvious explanation for why a file cannot be deleted is that another process is using it (I'm running on Windows), but I've no idea which process might be responsible. The fact that the problem cannot be reproduced consistently suggests a threading issue, but AFAIK, Maven plugins are run in a single thread. It's difficult to get any information about the cause of the problem, because the methods referred to above don't throw exceptions, they just return false.
Is there a way to programatically detect a locked file and the name of the process holding the lock? Alternatively, if anyone has other suggestion about how to go about debugging a problem such as this one, please send them on.
Thanks, Don
Handle can let you find out what processes have handles on files.
Sample output (it's a command line utility):
C:\Users\Jon\Downloads\Handle>handle Test.cs
Handle v3.42
Copyright (C) 1997-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
Test.exe pid: 6088 190: C:\Users\Jon\Test\Test.cs
精彩评论