Java 'file.delete()' Is not Deleting Specified File
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.
Java, cannot delete file on Windows
windows locks files that are currently in use. you cannot delete them. on windows, you cannot delete a jar file which your application is currently using.
Result of 'File.delete()' is ignored
The warning that you are seeing is telling you that file.delete()
returns a value, and that you are ignoring that returned value.
The issue is that you cannot know that the file was actually deleted unless you check that the call to file.delete()
returns true
.
So, to eliminate this warning, evaluate the return value, and appropriately handle the case where it is false
.
java file.delete() won't work
The evidence is clear that the reason the delete is failing is that your application still has the file open ... somewhere.
To resolve this, you need to figure out where you are opening the file, and make sure that you close it ... before you attempt to delete it. (I suspect that the problem is something to do with your Encrypt_Decrypt
class, and the way that you are using it. But that's just a guess.)
How do I delete files programmatically on Android?
Why don't you test this with this code:
File fdelete = new File(uri.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + uri.getPath());
} else {
System.out.println("file not Deleted :" + uri.getPath());
}
}
I think part of the problem is you never try to delete the file, you just keep creating a variable that has a method call.
So in your case you could try:
File file = new File(uri.getPath());
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}
However I think that's a little overkill.
You added a comment that you are using an external directory rather than a uri. So instead you should add something like:
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/images/media/2918");
Then try to delete the file.
Related Topics
Java's Date(...) Constructor Is Deprecated; What Does That Mean
Java Interfaces Methodology: Should Every Class Implement an Interface
Exception Thrown Inside Catch Block - Will It Be Caught Again
Hexadecimal to Integer in Java
What Does Biginteger Having No Limit Mean
Java Sax Parser Split Calls to Characters()
Java Class.Cast() VS. Cast Operator
Understanding Spring @Configuration Class
How to Tell Jackson to Ignore a Property for Which I Don't Have Control Over the Source Code
Increasing Heap Space in Eclipse: (Java.Lang.Outofmemoryerror)
Programmatically Change Log Level in Log4J2
Loading Raw 64-Byte Long Ecdsa Public Key in Java
Java Generics Wildcard Question: List<? Extends A>
Java.Lang.Outofmemoryerror: Java Heap Space in Maven
How to Print Escape Characters in Java