当我复制两个emtpy路径,为什么不抛出异常会发生什么? [英] What happens when I copy two emtpy paths and why doesn't it throw an exception?

查看:187
本文介绍了当我复制两个emtpy路径,为什么不抛出异常会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 = 

不应该抛出 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

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

推荐答案

您可能需要检查 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 有关空路径的含义的详细信息:

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.

这篇关于当我复制两个emtpy路径,为什么不抛出异常会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆