new File(“”)vs. new File(“。”):功能还是Bug?

Axel:

new File("")new File(".")产生相同的规范路径,但前一个对象是不可分解的。考虑下面的代码,以及两个对象如何返回相同的规范路径。该文档指出规范路径是“绝对的和唯一的”。但是只有用“。”创建的文件。论点实际上是有用的。

难道不应该在某个时候抛出异常吗?是在空字符串构造函数调用中(因为创建的对象似乎无效),还是至少在getCanonicalPath中(至少声明了IOException)?

import java.io.File;
import java.io.IOException;

public abstract class Test {

    public static void main(String[] args) throws Exception {
        testFile("");
        testFile(".");
    }

    private static void testFile(String arg) throws IOException {
        System.out.format("File constructor argument: \"%s\"\n", arg);
        File g = new File(arg);
      System.out.format("toString()            = \"%s\"\n", g.toString());
        System.out.format("getAbsolutePath()     = \"%s\"\n", g.getAbsolutePath());
        System.out.format("getAbsoluteFile()     = \"%s\"\n", g.getAbsoluteFile());
        System.out.format("getgetCanonicalPath() = \"%s\"\n", g.getCanonicalPath());
        System.out.format("getgetCanonicalFile() = \"%s\"\n", g.getCanonicalFile());
        System.out.format("exists()              = %s\n", g.exists());
        System.out.format("isDirectory()         = %s\n", g.isDirectory());
        System.out.println();
  }
}

以及它产生的输出:

File constructor argument: ""
toString()            = ""
getAbsolutePath()     = "C:\usr\workspace\Test"
getAbsoluteFile()     = "C:\usr\workspace\Test"
getgetCanonicalPath() = "C:\usr\workspace\Test"
getgetCanonicalFile() = "C:\usr\workspace\Test"
exists()              = false
isDirectory()         = false

File constructor argument: "."
toString()            = "."
getAbsolutePath()     = "C:\usr\workspace\Test\."
getAbsoluteFile()     = "C:\usr\workspace\Test\."
getgetCanonicalPath() = "C:\usr\workspace\Test"
getgetCanonicalFile() = "C:\usr\workspace\Test"
exists()              = true
isDirectory()         = true
年龄:

在将构造函数与空字符串一起使用时,您将创建一个具有两个属性的File实例:

  • 它实际上不存在。
  • 它的绝对路径名是“空抽象路径名”

使用File(“。”)时,您将创建另一个文件:

  • 它确实存在于文件系统上
  • 它的绝对路径名包含“。” 字符

此第二个文件存在,而不是第一个。因此,第二个文件是唯一应该遵守getCanonicalPath中解释的规则的文件:

表示现有文件或目录的每个路径名都具有唯一的规范形式。

由于第一个文件不是真实的,因此它们的规范路径相等是没有意义的。

因此,您所指出的行为不是错误。这是我们对JVM的期望。

您将在javadoc中找到所有信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Writing a text file in new format

Groovy new File命令失败

JAVA中的“ ClassName :: new” VS“ new ClassName()”

在不存在的子目录中创建文件(../new_folder/new_folder/new_file.ext)

Copy a specific percentage of each file in a directory to a new file

为什么Scala可以支持此功能:new PrintWriter(“ filename”){write(“ file contents”); 关 }?

DataOutputStream()VS DataOutputStream(new BufferedOutputStream())

分配:new(Foo)vs Foo {}

为什么“ new_file + =行+字符串”比“ new_file = new_file +行+字符串”这么快?

如何在Eclipse的File> New> _____菜单中添加“ Android Project”?

Sublime无法在宏中打开new_file

Listener for new file created in workspace in vscode extension api

如何获得JUnit测试?它不在file> new下吗?

new $this->table() vs. new tablename(); 动态类生成

在Go中使用new vs var

Malloc vs New-不同的填充

Async/Await vs new Promise(resolve,reject)

GameObject.name vs new GameObject(name)

new function(){}与new Function();

尝试创建文件file = new File(path)时为Null PointerException

File.info(string)/File::Info.new(string)`是否解析符号链接?

(echo'text'; cat file.txt)> new file.txt实际如何工作?

Java'原型'模式-new vs clone vs class.newInstance

添加要由Files.probeContentType(new File(“。ttf”)。toPath())识别的文件类型;

使用File.new在Ruby中“打开”文件是什么意思?

如何使新项目向导在Eclipse的File-> New-> Example ...中可见?

Tensorflow:TypeError:__new __()获得了意外的关键字参数'file'

什么是概念和规范文件,它们可以从android Studio中的file-> new创建?

JS封装问题:“ this.foo = new function(){...};” vs“ this.Bar = function(){..}; this.foo = new Bar();”