文件#delete(路径)和File#delete()之间的区别

时间:2021-10-11 03:24:56

I'm using Windows-7 with java 7 update 6 and found this weird (at least to me) behavior -
I have two files E:\delete1.txt and E:\delete2.txt both are read only files, when I try to delete file like following it gets deleted without any issues -

我使用windows 7和java 7更新6,发现了这个奇怪的(至少对我来说)行为——我有两个文件E:\delete1。txt和E:\ delete2。txt都是只读文件,当我试图删除文件时,它会被删除,没有任何问题

File file = new File("E:\\delete1.txt"); 
assertTrue(file.delete());

But when I delete file using nio API like following -

但是当我用nio API删除文件时就像下面这样

Path path = Paths.get("E:\\delete2.txt");
Files.delete(path);

It throws java.nio.file.AccessDeniedException.

它把java.nio.file.AccessDeniedException。

Why different behavior for same operation with old and new nio API?

为什么在使用新的和旧的nio API的相同操作上有不同的行为?

2 个解决方案

#1


10  

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.

正如这里所讨论的,问题是java.io。文件有许多奇怪的地方,特别是在Windows上。在这种情况下,它在删除文件之前重新设置文件属性,这就是为什么它不会像预期那样失败。这种行为可以追溯到10年前,所以现在改变是有风险的。它还有其他一些类似的奇怪之处,这正是为什么它没有被重新实现以使用新的API的原因之一。如果我们试图从命令窗口删除文件,那么windows会抛出相同的(拒绝访问)错误,但是文件会从资源管理器窗口删除。看起来文件#delete()实现错误,而新的文件#delete(Path)应该是首选。

#2


0  

Why different behavior for same operation with old and new nio API?

为什么在使用新的和旧的nio API的相同操作上有不同的行为?

Because exactly emulating the behaviour of the old API for similar operations was apparently not considered an important goal in the design of the new API. Given that the main goal of the nio filesystem API was to present a new API with some quite different underlying concepts and a lot of new capabilities, it seems pretty normal to me.

因为在新API的设计中,完全模拟类似操作的旧API的行为显然不被认为是一个重要的目标。考虑到nio文件系统API的主要目标是提供一个新的API,其中包含一些非常不同的底层概念和许多新功能,这对我来说似乎很正常。

#1


10  

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.

正如这里所讨论的,问题是java.io。文件有许多奇怪的地方,特别是在Windows上。在这种情况下,它在删除文件之前重新设置文件属性,这就是为什么它不会像预期那样失败。这种行为可以追溯到10年前,所以现在改变是有风险的。它还有其他一些类似的奇怪之处,这正是为什么它没有被重新实现以使用新的API的原因之一。如果我们试图从命令窗口删除文件,那么windows会抛出相同的(拒绝访问)错误,但是文件会从资源管理器窗口删除。看起来文件#delete()实现错误,而新的文件#delete(Path)应该是首选。

#2


0  

Why different behavior for same operation with old and new nio API?

为什么在使用新的和旧的nio API的相同操作上有不同的行为?

Because exactly emulating the behaviour of the old API for similar operations was apparently not considered an important goal in the design of the new API. Given that the main goal of the nio filesystem API was to present a new API with some quite different underlying concepts and a lot of new capabilities, it seems pretty normal to me.

因为在新API的设计中,完全模拟类似操作的旧API的行为显然不被认为是一个重要的目标。考虑到nio文件系统API的主要目标是提供一个新的API,其中包含一些非常不同的底层概念和许多新功能,这对我来说似乎很正常。