My java code is unable to delete files on the the system hard drive.
file.delete() function is called, it returns
Any ideas, why that might be happening?
File.delete() can fail to delete a file for many reasons including:
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
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. :)