- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I assume Files.deleteIfExists(filePath) returns true, false or FileNotFound
btw, frist.
Admin
Comments.deleteThisIfFristExists();
Admin
This made me chuckle way more than the code and article itself! Take a bow :)
Edit Admin
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!
Edit Admin
"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?"
Edit Admin
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?"
Admin
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.)
Admin
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