java - 当我复制两个空路径时会发生什么并且为什么它不抛出异常?

标签 java file path filesystems copy

我真的很想知道这段代码的作用:

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/

相关文章:

php - 在 PHP 中为完整路径设置 chmod

java - Executorservice worker 没有在单独的线程中运行

java - 房间数据库 - 外键约束失败错误代码 787

java - core-ktx 1.7.0 和 Hilt

file - perl6 IO::Handle没有printf方法,与文档不一致,或者我错过了什么?

python - python 系统路径故障排除

java - 为什么引导类加载器未加载 <JAVA_HOME>/jre/lib 目录中的 jar 文件

c - 将文件写入 .c 中的另一个文件

java - pom.xml 无法解析父级?

ruby-on-rails - 每当 gem 不执行任务时