我真的很想知道这段代码的作用:
scala> import java.nio.file._
import java.nio.file._
scala> Files.copy(Paths.get(""), Paths.get(""))
res0: java.nio.file.Path =
这不应该抛出NoSuchFileException
吗?
阅读JavaDoc揭示:
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.
但我不确定这是否是真正的原因,因为 Files.copy(Paths.get("a"), Paths.get("a"))
按预期失败。
最佳答案
您可能想检查http://download.oracle.com/javase/7/docs/api/java/nio/file/Paths.html当提供空字符串(它生成空路径)和 http://download.oracle.com/javase/7/docs/api/java/nio/file/Path.html 时 paths.get 会执行什么操作有关空路径含义的详细信息:
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.
所以看起来您的代码正在获取一个有效的空路径,然后认为源和目标是相同的。
关于java - 当我复制两个空路径时会发生什么并且为什么它不抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6831605/