Java代码无法删除文件

时间:2021-08-12 23:53:00

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。任何想法,为什么会发生这种情况?

7 个解决方案

#1


12  

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)
  • 该文件被另一个进程锁定,(或者甚至通过相同的进程,例如未关闭的FileOutputStream)
  • the file doesn't exist
  • 该文件不存在

#2


5  

File.delete() can return false if 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)

#3


2  

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后,我的文件被删除没有任何问题。我希望这可以帮助别人。

#4


1  

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?

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

#5


1  

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。

#6


1  

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

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

On Linux, use fuser.

在Linux上,使用热熔器。

#7


0  

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之外的任何其他驱动器中,然后运行您的代码。希望对你有效。 :)

#1


12  

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)
  • 该文件被另一个进程锁定,(或者甚至通过相同的进程,例如未关闭的FileOutputStream)
  • the file doesn't exist
  • 该文件不存在

#2


5  

File.delete() can return false if 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)

#3


2  

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后,我的文件被删除没有任何问题。我希望这可以帮助别人。

#4


1  

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?

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

#5


1  

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。

#6


1  

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

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

On Linux, use fuser.

在Linux上,使用热熔器。

#7


0  

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之外的任何其他驱动器中,然后运行您的代码。希望对你有效。 :)