What happens when I copy two empty paths and why d

2019-09-15 11:20发布

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条回答
家丑人穷心不美
2楼-- · 2019-09-15 11:45

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.

查看更多
登录 后发表回答