使用 Java 删除文件时出现问题 (apache commons io)

发布于 2024-11-06 08:40:31 字数 433 浏览 0 评论 0原文

我通过 JNI 调用 C++ 方法,该方法创建两个文件。给定目录中的文本日志文件和 pdf 文件。我想在执行 JNI 方法之前删除这些文件(如果存在)。

我正在使用 Apache commons.io (FileUtils.forceDelete(File file)) 来实现此目的。当我执行时,我收到 IOException:

java.io.IOException: Unable to delete file: D:\Folder\file.log

在使用 File.canWrite() 方法触发删除方法之前检查文件的可写状态。它对于文件和父目录都返回 true。

您知道为什么我在删除文件时遇到问题吗?据我所知,创建文件的 C++ 方法在方法完成后将关闭或解锁它们。无论如何,我无权访问 C++ 代码的源代码,因此我无法检查情况是否属实或修改代码。

I am calling a C++ Method via JNI which creates two files. A text log file and a pdf file in a given directory. I want to delete these files (if they exist) before executing the JNI method.

I am using Apache commons.io (FileUtils.forceDelete(File file)) for that. When I execute I get an IOException:

java.io.IOException: Unable to delete file: D:\Folder\file.log

I check the writable state of the file before fireing the delete method with the File.canWrite() method. It returns true for both the file and the parent dir.

Do you have an idea why I have problems deleting the file? As far as I know the C++ method which is creating the files is closing or unlocking them after the method finishes. Anyway, I don't have access to the source code of the C++ code so I can't check if that is really the case or modify the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-11-13 08:40:31

几乎可以肯定它已被另一个进程锁定。如果它是在操作系统级别锁定的另一个进程(假设您使用文本编辑器打开了该文件),那么您不会有太多运气。如果其他东西锁定了文件,即使是 Windows 资源管理器也可能无法删除该文件。不过,请查看 java.nio .channels.FileLock 用于相关API调用。

It is almost certainly locked by another process. If it is another process locking at the OS level (say you had the file open it a text editor) then you won't have much luck. Even windows explorer can fail to delete a file if something else is locking it. However have a look at java.nio.channels.FileLock for the relevant API calls.

苯莒 2024-11-13 08:40:31

最有可能的是,另一个进程使 file.log 保持打开状态,这将阻止其被删除。

Most likely, another process is keeping file.log open, which would prevent it from being deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文