java Java代码无法删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3333848/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:21:36  来源:igfitidea点击:

Java Code unable to delete file

java

提问by Vivek

My java code is unable to delete files on the the system hard drive.

我的 java 代码无法删除系统硬盘驱动器上的文件。

Whenever file.delete()function is called, it returns false. Any ideas, why that might be happening?

每当file.delete()调用函数时,它都会返回false。任何想法,为什么会发生这种情况?

回答by krock

File.delete()can fail to delete a file for many reasons including:

File.delete()由于多种原因,可能无法删除文件,包括:

  • you don't have correct permissions to delete the file
  • the file represents a directory and the directory is not empty
  • the file is locked by another process, (or even by the same process in say an unclosed FileOutputStream)
  • the file doesn't exist
  • 您没有删除文件的正确权限
  • 该文件代表一个目录,该目录不为空
  • 该文件被另一个进程锁定,(或者甚至被同一个进程锁定在一个未关闭的进程中FileOutputStream
  • 该文件不存在

回答by Greg Hewgill

File.delete()can return falseif you are trying to delete a directory that is not empty, or the named file simply doesn't exist at the time of the call.

File.delete()false如果您尝试删除非空目录,或者在调用时命名文件根本不存在,则可以返回。

(if there is a permission issue, a SecurityException is thrown)

(如果存在权限问题,则抛出 SecurityException)

回答by ccellist

I had the same issue in my code and found that the culprit was actually an unclosed FileInputStream. After closing that FIS my file deleted without any problems. I hope this helps someone.

我在我的代码中遇到了同样的问题,发现罪魁祸首实际上是一个未关闭的 FileInputStream。关闭 FIS 后,我的文件删除没有任何问题。我希望这可以帮助别人。

回答by Stephen

The usual reasons are insufficient permissions (although normally that would throw an exception), trying to delete a non-existant file or trying to delete a non-empty directory. Are you totally sure that you have permissions to delete the file you are trying to delete?

通常的原因是权限不足(尽管通常会引发异常)、尝试删除不存在的文件或尝试删除非空目录。您是否完全确定您有权删除要删除的文件?

回答by Esko Luontola

Some process might be reading/writing the file, so that it is locked. Or then your process does not have permissions to delete the file. If the file is a directory, all files inside it must be deleted first before the directory can be deleted. And finally there is the situation that the file does not exist, so the delete method will return false.

某些进程可能正在读/写文件,因此它被锁定。否则您的进程没有删除文件的权限。如果文件是目录,则必须先删除其中的所有文件,然后才能删除该目录。最后还有文件不存在的情况,所以delete方法会返回false。

回答by Aaron Digulla

Windows? Use the Process Explorerto search for all processes which keep a handle (lock) on the file (or if this is a directory on any file inside of it).

视窗?使用进程资源管理器搜索在文件上保留句柄(锁)的所有进程(或者如果这是其中任何文件的目录)。

On Linux, use fuser.

在 Linux 上,使用fuser.

回答by Prakhar Gurawa

Make sure the file is not currently is use: For example i was trying to delete a file using

确保当前未使用该文件:例如,我试图使用

f2.delete()

But it was unable to do as i was using BufferReader,FileWriter,BufferWriter etc.Close all those and then try.

但它无法做到,因为我正在使用 BufferReader、FileWriter、BufferWriter 等。关闭所有这些然后尝试。

buffer.close()
writer.close()

回答by Pavan Dave

You might be trying to delete any file exists in C: Drive and on that you might not have the permissions to do so. Try to put it inside any other drive than C: and then run your code. Hope it works for you. :)

您可能正在尝试删除 C: Drive 中存在的任何文件,并且您可能没有这样做的权限。尝试将它放在除 C: 之外的任何其他驱动器中,然后运行您的代码。希望对你有效。:)