无法在 Java 中删除文件,因为它是在 Java Platform SE 二进制文件中打开的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19329223/
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
Can not delete file in java beause it's opened in Java Platform SE binary
提问by Andy
I have three directories A, B & C on windows. I have a file that exists in directory A. I want to do the following tasks
我在 Windows 上有三个目录 A、B 和 C。我有一个存在于目录 A 中的文件。我想执行以下任务
- Copy it to directory B
- Delete it from directory A (This WORKED since the file was not being held by any process)
- Copy it to directory C
- Delete it from directory B (NOT Work)
- 复制到目录B
- 从目录 A 中删除它(这是有效的,因为该文件未被任何进程保存)
- 复制到目录C
- 从目录 B 中删除它(不工作)
Steps 1,2,3 work fine but it does not work with step 4. The file exists and can write, read, execute. When I open windows explorer and try to delete the file in directory B manually, it said the action can't be completed because it's opened in java platform SE binary. Below is my code for copying the file
步骤 1、2、3 工作正常,但不适用于步骤 4。文件存在并且可以写入、读取、执行。当我打开Windows资源管理器并尝试手动删除目录B中的文件时,它说该操作无法完成,因为它是在java平台SE二进制文件中打开的。下面是我复制文件的代码
FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
I'm using Java 6. Do you know how I can accomplish step 4?
我正在使用 Java 6。你知道我如何完成第 4 步吗?
回答by Kong
Why not use a library like Apache Commons IO (FileUtils)?
为什么不使用像 Apache Commons IO (FileUtils) 这样的库?
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
File a = new File("A/file.txt");
File b = new File("B/file.txt");
File c = new File("C/file.txt");
FileUtils.copyFile(a, b);
a.delete();
FileUtils.copyFile(b, c);
b.delete();
回答by Bhanu Kaushik
Try This:
尝试这个:
Code
代码
public void foo(){
File afile =new File("A\Afile.txt");
File bfile =new File("B\Bfile.txt");
InputStream inStream = new FileInputStream(afile);
OutputStream outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("File Copied");
if(afile.delete()){
System.out.println(file.getName() + " deleted!");
}else{
System.out.println("Delete failed.");
}
}
Please make sure you use proper try and catch clauses
请确保使用正确的 try 和 catch 子句
回答by Lajos Arpad
Here, hereand hereyou can see how you can delete a file in Java.
在这里,在这里和这里您可以看到如何在 Java 中删除文件。
As about your error message it is indicating that you try to delete the file while it is still loaded. You either try to delete the file before the garbage collector does its job or you attempt to delete the file while some of your objects are still using it. Make sure that you are not referencing the file in your code when you want to delete it.
至于您的错误消息,它表明您尝试在文件仍在加载时将其删除。您要么尝试在垃圾收集器完成其工作之前删除该文件,要么尝试在您的某些对象仍在使用该文件时删除该文件。当您要删除它时,请确保您没有在代码中引用该文件。