java 无法删除原始文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16104343/
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
Failed to delete original file
提问by user739115
we are running jar using batch file as window service and place xml files in c:/processed
directory;
我们正在使用批处理文件作为窗口服务运行 jar 并将 xml 文件放在c:/processed
目录中;
if xml has an errorneous format then move it to error folder; we are using below method to move the file to error folder but getting below exception.
如果 xml 格式错误,则将其移至错误文件夹;我们正在使用以下方法将文件移动到错误文件夹,但低于异常。
I think when the process is running its not able to move the file to error folder .
我认为当进程运行时它无法将文件移动到错误文件夹。
FileUtils.moveToDirectory("a.xml", "c:/processed/error", false);
exception in movedErrorFolder: Failed to delete original file 'c:/processed/a.xml' after copy to 'c:/processed/error/a.xml'
movedErrorFolder 中的异常:复制到“c:/processed/error/a.xml”后无法删除原始文件“c:/processed/a.xml”
回答by mki
- The java process does not have the right to delete the file, but if you can write on it then you can delete it.
- The file is locked by another process. This occurs typically when you write a csv open with Excel.
- The file is locked by the java process self, close() the file before moving it.
- java进程没有删除文件的权限,但是如果你可以在上面写,那么你就可以删除它。
- 该文件被另一个进程锁定。这通常在您编写使用 Excel 打开的 csv 时发生。
- 该文件被 java 进程自身锁定,在移动文件之前关闭()该文件。
回答by user2645432
After reader.close() put Thread.sleep() - It works like charm
在 reader.close() 放置 Thread.sleep() 之后 - 它就像魅力一样
reader.close();
// Thread is Explicitly made to Sleep as Threads were shared and files were not getting Moved.
Thread.sleep(10000);
回答by Chris Gerken
Another exception giving more details is probably nested in the Exception, held by the cause property
另一个提供更多细节的异常可能嵌套在由 cause 属性持有的异常中
回答by user2765879
1) Make sure you do not have the xml open in an editor/viewer or used by another process.
2) make sure you .close() before moving the file.
1) 确保您没有在编辑器/查看器中打开 xml 或被其他进程使用。
2) 确保在移动文件之前 .close() 。
回答by Shady Sherif
Check if you opened a file inside the directory for read or write and you didn't call .close();
before trying to delete the parent directory.
检查您是否在目录中打开了一个文件进行读取或写入,并且.close();
在尝试删除父目录之前没有调用。
回答by Amr Lotfy
Could you try this method from apache:
你能从 apache 上试试这个方法吗:
void org.apache.commons.io.FileUtils.moveFileToDirectory(File srcFile, File destDir, boolean createDestDir)
e.g.
例如
use this import
使用这个导入
import org.apache.commons.io.FileUtils;
and your code would be like this:
你的代码是这样的:
FileUtils.moveFileToDirectory(new File("c:/processed/a.xml"), new File("c:/processed/error"), false);
回答by Anil
I was struggling with this error since last 24 hours. None of the answers above worked for me. My operating system is Windows 7 64 bit and I am using JDK 6. I tried methods FileUtils.moveToDirectory as well as file.delete.
自过去 24 小时以来,我一直在努力解决此错误。上面的答案都不适合我。我的操作系统是 Windows 7 64 位,我使用的是 JDK 6。我尝试了 FileUtils.moveToDirectory 和 file.delete 方法。
I suspected it had something to do with Java. I uninstalled and reinstalled JDK 6 (I ran the installer as an Administrator just to be sure) and restarted my machine and Eureka, the error disappeared and things started working.
我怀疑它与Java有关。我卸载并重新安装了 JDK 6(我以管理员身份运行安装程序只是为了确定)并重新启动我的机器和 Eureka,错误消失了,一切开始工作。
Now don't ask me why I am still using JDK 6 in 2017 (Some enterprise software ****)
现在别问我为什么2017年还在用JDK 6(一些企业软件****)