无法删除临时文件夹(有时)
当我启动应用程序时,我创建一个临时文件夹:
public static File createTempDir(String name) throws IOException {
File tempDir = File.createTempFile(name, "");
if (!(tempDir.delete())) {
throw new IOException("could not delete" + tempDir.getAbsolutePath());
}
if (!(tempDir.mkdir())) {
throw new IOException("could not create" + tempDir.getAbsolutePath());
}
tempDir.deleteOnExit();
return tempDir;
}
在会话期间,用户可能会加载文件。因此,旧的临时目录将被删除,并根据加载的文件的 ID 创建新的临时目录。
在删除旧临时目录的加载过程中,我有时会得到一个:
java.io.IOException:无法删除文件:
以下是删除旧临时文件夹的方式:
public void cleanup(String tmpPath) {
File tmpFolder = new File(tmpPath);
if (tmpFolder != null && tmpFolder.isDirectory()) {
try {
FileUtils.deleteDirectory(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
其中FileUtils是:org.apache.commons.io.FileUtils。通常临时文件夹的内容是:
mytempfolder_uuid |->我的子文件夹 |-> myImage.jpg
错误是:
java.io.IOException: 无法删除文件: C:\Users\xxx\AppData\Local\Temp\mytempfolder_uuid\mysubfolder\myImage.jpg
我曾尝试调试应用程序并在删除之前执行操作验证上述图像实际上位于指定文件夹中。
令人讨厌的是,这种情况只是偶尔发生。我已确保不要在任何其他应用程序中打开临时文件夹中的文件夹/文件。有什么想法/建议吗?
When I start my application I create a temp folder:
public static File createTempDir(String name) throws IOException {
File tempDir = File.createTempFile(name, "");
if (!(tempDir.delete())) {
throw new IOException("could not delete" + tempDir.getAbsolutePath());
}
if (!(tempDir.mkdir())) {
throw new IOException("could not create" + tempDir.getAbsolutePath());
}
tempDir.deleteOnExit();
return tempDir;
}
During a session a user might load a file. As a result the old temp dir is deleted and a new is created based on the ID of the file loaded.
During load where the old temp dir is deleted I sometimes get a:
java.io.IOException: Unable to delete file:
Here is how the old temp folder is deleted:
public void cleanup(String tmpPath) {
File tmpFolder = new File(tmpPath);
if (tmpFolder != null && tmpFolder.isDirectory()) {
try {
FileUtils.deleteDirectory(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
where FileUtils is: org.apache.commons.io.FileUtils. Typically the content of the temp folder is:
mytempfolder_uuid
|-> mysubfolder
|-> myImage.jpg
And the error is:
java.io.IOException: Unable to delete file: C:\Users\xxx\AppData\Local\Temp\mytempfolder_uuid\mysubfolder\myImage.jpg
I have tried to debug the application and before the delete operation is executed verified that the above image is actually located in the specified folder.
The nasty thing is that it only happens sometimes. I have made sure not to have the folder/files in the temp folder open in any other applications. Any ideas/suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法删除打开的文件,也无法删除包含文件的目录。您必须确保目录中的所有文件都已关闭。
You cannot delete files which are open and you can't delete a directory which contains a file. You have to ensure all files in the directory are closed.
我建议你使用 Guava 库。它有一个方法
Files.createTempDir()
完全符合您的需要:I'd suggest you use the Guava library. It has a method
Files.createTempDir()
that does exactly what you seem to need:在删除之前尝试删除临时文件夹中的文件。尝试
使用deleteOnExit之类的东西并不是一个好主意......
干杯!
try deleting the files in the temp folder before deleting it. Try somethng like
also using deleteOnExit is not a very good idea...
cheers!
然后你可以像这样使用它:
deleteDir("C:\\MyFolder\\subFolder\\")
and then you can use it like:
deleteDir("C:\\MyFolder\\subFolder\\")