0

i am facing the issue Exceptional return value of java.io.File.delete() ignored. when i delete the file if exist.

Set<File> sourceFiles = new HashSet<File>();
sourceFile = path + folder + File.separator + fileName + ".txt";
sourceFiles.add(new File(sourceFile));

 for (File file : sourceFiles) {
        if (file.exists()) {
          file.delete();
        }
      }

Any Help

Jony Mittal
  • 151
  • 6
  • 23
  • 1
    [`File#delete()`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/io/File.html#delete()) returns false if the file could not be deleted for whatever reason. – Slaw Jun 05 '20 at 09:51
  • What 'exceptional return value'? Do you mean `false`? If so, *test* it. – user207421 Jun 05 '20 at 10:05

1 Answers1

2

file.delete(); returns a boolean which states whether the deletion was successful.

You should check if the deletion of the file was successful, e.g.

boolean success = file.delete();
if (success) {
  // everything ok
} else {
  // file could not be deleted
}
svdragster
  • 141
  • 2
  • 7