Difference between Files#delete(Path) and File#delete()

As discussed here – The issue is that java.io.File has many oddities, on Windows in
particular. In this case it resets the file attributes before it deletes
the file so this is why it does not fail as might be expected. It is
behavior that dates back >10 years and so would be risky to change now.
It has several other oddities like this, just one of the reason why it
wasn’t re-implemented to use the new API.

If we try to delete the file from command window then windows throws the same (Access denied) error but file gets deleted from explorer window. It appears the File#delete() has a wrong implementation and new Files#delete(Path) should be preferred instead.

Leave a Comment