• Ginssuart (unregistered)

    I assume Files.deleteIfExists(filePath) returns true, false or FileNotFound

    btw, frist.

  • Michael R (unregistered)

    Comments.deleteThisIfFristExists();

  • Robin (unregistered) in reply to Ginssuart

    This made me chuckle way more than the code and article itself! Take a bow :)

  • Lothar (unregistered)
    Comment held for moderation.
  • (nodebb)

    There should be a try/catch at the end of this block that checks for the file and throws a "File Not Deleted" exception which, of course, needs to be captured and swallowed with no log entry, no UI message, and no action taken.

    Or, maybe the "File Not Deleted" exception should create the file and then re-run the delete. Y'know - to make sure it really gets deleted!

  • (nodebb)

    "Or, maybe the "File Not Deleted" exception should create the file and then re-run the delete. Y'know - to make sure it really gets deleted!" The file equivalent to an "Upsert?"

  • 516052 (unregistered)
    Comment held for moderation.
  • (nodebb)

    What it really needs is user confirmation -- "Do you really want to delete this file?", followed by "Are you really sure you want to delete this file?"

  • D Boone (unregistered)

    Way, way back in the early days of Microsoft Foundation Classes (MFC), trying to delete a file that didn't exist threw an exception which the delete code caught and then translated into the error code. But that exception handling was very expensive, it caused a long pause while waiting for MFC to do its thing, so we did that kind of test to see if the file existed before trying to delete it. (As a bonus, we didn't try to use the MFC File.Exists(), we asked for the file's attributes because File.Exists() did a FindFirst/FindNext even for just one file. The good old days.)

  • r (unregistered)

    if (Files.exists(filePath)) { Files.deleteIfExists(filePath); } if (Files.exists(filePath)) { System.err.println("Didn't work! File still exists!!!"); }

    ... THERE!!! Fixed it for you. After all, you can never be too carefull. Or too redundant! :-D

Leave a comment on “Repeating Your Existence”

Log In or post as a guest

Replying to comment #692551:

« Return to Article