当前位置: 动力学知识库 > 问答 > 编程问答 >

Java Code unable to delete file

问题描述:

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

Whenever file.delete() function is called, it returns false.

Any ideas, why that might be happening?

网友答案:

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

  • 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
网友答案:

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.

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

网友答案:

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.

网友答案:

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?

网友答案:

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.

网友答案:

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).

On Linux, use fuser.

网友答案:

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. :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: