java - java中访问资源的不同字符串

标签 java windows

我正在学习如何独立访问java中的资源。因此,我使用 File 类尝试了下面列出的不同方法。

    System.out.println("File .: " + new File(".").getAbsolutePath());
    System.out.println("File /:  " + new File("/").getAbsolutePath());
    System.out.println("File /.:  " + new File("/.").getAbsolutePath());
    System.out.println("File ./:  " + new File("./").getAbsolutePath());
    System.out.println("File ..:  " + new File("..").getAbsolutePath());
    System.out.println("File ../:  " + new File("../").getAbsolutePath()); // why / not printed at the end?
    System.out.println("File /..:  " + new File("/..").getAbsolutePath()); 
    System.out.println("File /../:  " + new File("/../").getAbsolutePath()); // why / also not here
    System.out.println("File //:  " + new File("//").getAbsolutePath());  // why output is only \\  No path even
    System.out.println("File ..//:  " + new File("..//").getAbsolutePath()); // why not printed // at the end?
    System.out.println("File //..//:  " + new File("//..//").getAbsolutePath()); // why output \\.. only. No path even

输出如下所示。

File .: D:\8th Semester\Java\CMDProjects\ClassPathTest\out\.
File /:  D:\
File /.:  D:\.
File ./:  D:\8th Semester\Java\CMDProjects\ClassPathTest\out\. // I'm expecting / at the end too.
File ..:  D:\8th Semester\Java\CMDProjects\ClassPathTest\out\..
File ../:  D:\8th Semester\Java\CMDProjects\ClassPathTest\out\.. // same above question
File /..:  D:\..
File /../:  D:\.. // same above question
File //:  \\            // Here why D:\ path not printed?
File ..//:  D:\8th Semester\Java\CMDProjects\ClassPathTest\out\..  
File //..//:  \\..        //strange same above.

然后我用 classgetResource() 方法尝试了同样的事情。它的工作原理与预期相同,但在最后三行我通过一一检查发现了异常,为什么?

    URL dot = Test.class.getResource(".");
    URL dotS = Test.class.getResource("./");
    URL S = Test.class.getResource("/");
    URL Sdot = Test.class.getResource("/.");
    URL ddot = Test.class.getResource("..");
    URL ddotS = Test.class.getResource("../");
    URL sdd = Test.class.getResource("/.."); 
    URL sdds = Test.class.getResource("/../");
    URL ss = Test.class.getResource("//");

    System.out.println("getR .: " + dot.toString());        
    System.out.println("getR /:" + S.toString());
    System.out.println("getR /.:" + Sdot.toString());
    System.out.println("getR ./:" + dotS.toString());
    System.out.println("getR ..:" + ddot.toString());
    System.out.println("getR ../:" + ddotS.toString());
    // System.out.println("getR /..:" + sdd.toString());  // Exception Here
    // System.out.println("getR /../:" + sdds.toString()); // Exception Here
    // System.out.println("getR //:" + ss.toString());     // Exception Here

我想问为什么class的方法getResource()会抛出NullPointer异常?为什么上面的File的输出不同?请参阅我编写的顶部代码//为什么?

我使用的是 Windows 10。

正如我已经提到的,我正在学习以不同方式访问资源。如果您有任何有用的链接,也请分享。

最佳答案

空指针异常是因为 Test.class.getResource() 为最后三个路径字符串返回 null。每当找不到指定的资源时就会发生这种情况。出于安全原因,Java 代码不允许访问(甚至了解)类路径之外的资源。路径“/”映射到用于加载类Test的类路径的根目录。从你的输出来看,这是 D:\8th Semester\Java\CMDProjects\ClassPathTest (或者可能是 D:\8th Semester\Java\CMDProjects\ClassPathTest\out;您不会报告实验第二部分的输出)。

至于为什么尾部斜杠从文件路径中消失,这是 File#toString() 应该如何工作的一部分。根据文档,File#toString()返回与 File#getPath() 相同的字符串。 docs for that method ,依次说:

The resulting string uses the default name-separator character to separate the names in the name sequence.

请注意,名称分隔符用于分隔名称。它不保留尾随分隔符(不分隔任何内容)。

我不确定为什么 "//..//" 的绝对路径是 "\\.."。 (或者,就此而言,为什么 "//.." 结果是 "\\"。很容易理解为什么尾随 // > 被抑制;这些是不分隔任何内容的名称分隔符。在我的 Mac 上,这两个字符的输出都是 "/.."。这种行为显然与操作系统相关。我怀疑在 Windows 上,前导“//”代表计算机的网络路径,并且计算机名称不存在。

关于java - java中访问资源的不同字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48833450/

相关文章:

java - 在 Java 中读取 HTML 时遇到问题

java - 在 Java 中使用 CTRL + A 选择更多文本字段

java - 在 Hibernate 中使用 Load() 加载任意对象

python - 在 python 中使用 Windows shell '>' 将输出重定向到文本文件

c - 如何在Windows中使用系统调用从C打开图像?

java - 在 install4j 应用程序中强制更新

java - 我正在寻找优雅地停止 java 线程并发现了这一点,但我不知道如何检查这种情况的示例

java - 创建字节数组缓存

windows - 有没有办法在多GPU环境中以编程方式选择渲染GPU? ( Windows )

windows - 在 Windows 上开发 Linux 软件