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

Alan Bateman Alan.Bateman at oracle.com
Wed Aug 29 10:42:14 UTC 2012

On 29/08/2012 11:15, Premraj wrote:
> I was trying to shift from old file API to NIO API (using JDK 7 update 6)
> but I found that on Windows (I have Windows 7) platform if I have read only
> file then File#delete() call will delete the file while Files#delete(Path)
> fails with exception (java.nio.file.AccessDeniedException), I'm not sure
> whether this is known issue but since Files#delete(Path) offers better
> error handling I would like to use Files#delete(Path).
> My question is - Is there any workaround for this problem in NIO API or
> should I stick to old File API because of this. Also I was curios why
> Files#delete(Path) throws exception but File#delete() executes successfully.
I would expect that if you try to delete the file in Explorer or in a 
command window that you also get an "Access denied" error.

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.


More information about the core-libs-dev mailing list