问题
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