What happens when I copy two empty paths and why doesn't it throw an exception?

天大地大妈咪最大 提交于 2019-12-25 08:41:04

问题


I'm really wondering what this code does:

scala> import java.nio.file._
import java.nio.file._

scala> Files.copy(Paths.get(""), Paths.get(""))
res0: java.nio.file.Path = 

Shouldn't that throw a NoSuchFileException?

Reading the JavaDoc reveals:

By default, the copy fails if the target file already exists or is a symbolic link, except if the source and target are the same file, in which case the method completes without copying the file.

But I'm not sure this is the true cause, because Files.copy(Paths.get("a"), Paths.get("a")) fails as expected.


回答1:


You might want to check http://download.oracle.com/javase/7/docs/api/java/nio/file/Paths.html for what paths.get does when provided an empty string (it generates an empty path) and http://download.oracle.com/javase/7/docs/api/java/nio/file/Path.html for details on what an empty path means:

A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system.

So it looks like your code is getting an empty path which is valid and then considers the source and destination to be the same.



来源:https://stackoverflow.com/questions/6831605/what-happens-when-i-copy-two-empty-paths-and-why-doesnt-it-throw-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!