Java 'file.delete()' 不删除指定的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4485716/
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
Java 'file.delete()' Is not Deleting Specified File
提问by Kimberley Lloyd
This is currently what I have to delete the file but it's not working. I thought it may be permission problems or something but it wasn't. The file that I am testing with is empty and exists, so not sure why it doesn't delete it.
这是目前我必须删除的文件,但它不起作用。我认为这可能是权限问题或其他问题,但事实并非如此。我正在测试的文件是空的并且存在,所以不确定为什么不删除它。
UserInput.prompt("Enter name of file to delete");
String name = UserInput.readString();
File file = new File("\Files\" + name + ".txt");
file.delete();
Any help would be GREATLY appreciated!
任何帮助将不胜感激!
I now have:
我现在有:
File file = new File(catName + ".txt");
String path = file.getCanonicalPath();
File filePath = new File(path);
filePath.delete();
To try and find the correct path at run time so that if the program is transferred to a different computer it will still find the file.
尝试在运行时找到正确的路径,以便在将程序传输到另一台计算机时仍能找到该文件。
采纳答案by Goran Jovic
Be sure to find out your current working directory, and write your filepath relative to it.
一定要找出你当前的工作目录,并写出相对于它的文件路径。
This code:
这段代码:
File here = new File(".");
System.out.println(here.getAbsolutePath());
... will print out that directory.
...将打印出该目录。
Also, unrelated to your question, try to use File.separator
to remain OS-independent. Backslashes work only on Windows.
另外,与您的问题无关,请尝试使用File.separator
以保持独立于操作系统。反斜杠仅适用于 Windows。
回答by Stephen C
I suspect that the problem is that the path is incorrect. Try this:
我怀疑问题是路径不正确。尝试这个:
UserInput.prompt("Enter name of file to delete");
String name = UserInput.readString();
File file = new File("\Files\" + name + ".txt");
if (file.exists()) {
file.delete();
} else {
System.err.println(
"I cannot find '" + file + "' ('" + file.getAbsolutePath() + "')");
}
回答by Maha
I got the same problem! then realized that my directory was not empty. I found the solution in another thread: not able to delete the directory through Java
我遇到了同样的问题!然后意识到我的目录不是空的。我在另一个线程中找到了解决方案:无法通过Java删除目录
/**
* Force deletion of directory
* @param path
* @return
*/
static public boolean deleteDirectory(File path) {
if (path.exists()) {
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i]);
} else {
files[i].delete();
}
}
}
return (path.delete());
}
回答by user2926391
The problem could also be due to any output streams that you have forgotten to close. In my case I was working with the file before the file being deleted. However at one place in the file operations, I had forgotten to close an output stream that I used to write to the file that was attempted to delete later.
问题也可能是由于您忘记关闭的任何输出流。就我而言,我在删除文件之前正在处理该文件。然而,在文件操作的某个地方,我忘记关闭我用来写入文件的输出流,该文件稍后尝试删除。
回答by Abdul Ahad
In my case it was the close() that was not executing due to unhandled exception.
在我的情况下,由于未处理的异常而没有执行 close() 。
void method() throws Exception {
FileInputStream fis = new FileInputStream(fileName);
parse(fis);
fis.close();
}
Assume exception is being thrown on the parse(), which is not handled in this method and therefore the file is not closed, down the road, the file is being deleted, and that delete statement fails, and do not delete.
假设在 parse() 上抛出异常,在此方法中没有处理,因此文件没有关闭,在这条路上,文件正在被删除,并且删除语句失败,不要删除。
So, instead I had the code like this, then it worked...
所以,相反,我有这样的代码,然后它起作用了......
try {
parse(fis);
}
catch (Exception ex) {
fis.close();
throw ex;
}
so basic Java, which sometimes we overlook.
如此基本的 Java,有时我们会忽略它。
回答by Anurag Mishra
If you want to delete file first close all the connections and streams. after that delete the file.
如果要删除文件,请先关闭所有连接和流。之后删除文件。
回答by chatala.Akhileswarakumar
Problem is that check weather you have closed all the streams or not if opened close the streams and delete,rename..etc the file this is worked for me
问题是检查天气是否关闭了所有流,如果打开关闭流并删除,重命名..等文件,这对我有用
回答by NoteBender
I made the mistake of opening a BufferedReader like:
我犯了打开 BufferedReader 的错误,例如:
File f = new File("somefile.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
...and of course I could not execute the f.delete()
because I wrapped the
FileReader instead of instantiating its own variable where I could explicitly close it. Duh...
...当然我无法执行,f.delete()
因为我包装了 FileReader 而不是实例化它自己的变量,我可以明确地关闭它。呃...
Once I coded:
一旦我编码:
File f = new File("somefile.txt");
FileReader fread = new FileReader(f);
BufferedReader br = new BufferedReader(fread);
I could issue a br.close(); br=null; fread.close(); fread=null;
and the f.delete()
worked fine.
我可以发出 abr.close(); br=null; fread.close(); fread=null;
并且f.delete()
工作正常。
回答by ABHISHEK BHARDWAJ
Try closing all the FileOutputStream/FileInputStream
you've opened earlier in other methods ,then try deleting ,worked like a charm.
尝试关闭FileOutputStream/FileInputStream
您之前在其他方法中打开的所有内容,然后尝试删除,就像一个魅力。
回答by Tom Rutchik
In my case I was processing a set of jar files contained in a directory. After I processed them I tried to delete them from that directory, but they wouldn't delete. I was using JarFile to process them and the problem was that I forgot to close the JarFile when I was done.
就我而言,我正在处理目录中包含的一组 jar 文件。处理完它们后,我尝试从该目录中删除它们,但它们不会删除。我正在使用 JarFile 来处理它们,问题是我在完成后忘记关闭 JarFile。