We need to call file.exists()
before file.delete()
before we can delete a file E.g.
File file = ...;
if (file.exists()){
fil
Apache Commons IO's FileUtils offers FileUtils.deleteQuietly
:
Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories. The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- No exceptions are thrown when a file or directory cannot be deleted.
This offers a one-liner delete call that won't complain if the file fails to be deleted:
FileUtils.deleteQuietly(new File("test.txt"));