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

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

问题描述

  scala> import java.nio.file._ 
import java.nio.file._

scala> Files.copy(Paths.get(),Paths.get())
res0:java.nio.file.Path =

不应该抛出 NoSuchFileException



JavaDoc 显示:


默认情况下,如果目标文件已经存在,或者是
符号链接,除非源文件和目标文件是相同的文件,否则在
中这种情况下方法完成而不复制文件。

但是我不确定这是真正的原因,因为 Files.copy(Paths.get a),Paths.get(a))无法正常工作。

解决方案

您可能需要查看 http://download.oracle.com/javase / 7 /做cs / api / java / nio / file / Paths.html 提供了一个空字符串(它产生一个空的路径)和http://download.oracle.com/javase/7/docs/api/java/nio/file/Path.html有关空路径的详细信息:


如果Path仅由一个$ b $组成b名称为空的元素。使用空路径访问文件是相当于访问文件系统默认目录的


你的代码得到一个空的路径是有效的,然后认为源和目的地是相同的。


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.

解决方案

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.

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

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