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

java中访问资源的不同字符串,java,windows,Java,Windows,我正在学习如何独立访问java中的资源。因此,我使用Fileclass尝试了下面列出的不同方法 System.out.println("File .: " + new File(".").getAbsolutePath()); System.out.println("File /: " + new File("/").getAbsolutePath()); System.out.println("File /.: " + new File("/.").getAbsol

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

    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.
然后我用
class
getResource()
方法尝试了同样的方法。它的工作原理和预期的一样,但在最后三行中,我通过逐个检查发现异常,为什么

    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
异常?为什么上面的
文件的输出不同?查看我编写的顶层代码//为什么

我用的是Windows10


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

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

至于为什么尾随斜杠会从文件路径中消失,这是
file#toString()
工作原理的一部分。根据文档,返回与
文件#getPath()
相同的字符串。反过来,他们说:

结果字符串使用默认名称分隔符分隔名称序列中的名称

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


我不确定为什么
“//../”
的绝对路径是
“\\..”
。(或者,就这一点而言,为什么
“//…”
会导致
“\\\”
。很容易理解为什么尾部的
/
会被抑制;它们是名称分隔符,不会分隔任何内容。在我的Mac电脑上,这两个字符的输出都是
“/…”
。此行为显然依赖于操作系统。我怀疑在Windows上,前导的
“/”
表示到计算机的网络路径,而计算机名却不存在。

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

至于为什么尾随斜杠会从文件路径中消失,这是
file#toString()
应该如何工作的一部分。根据文档,返回与
file#getPath()
相同的字符串。反过来,返回的字符串是:

结果字符串使用默认名称分隔符分隔名称序列中的名称

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


我不知道为什么
“//../”
的绝对路径是
“\\\…”
(或者,就这一点而言,为什么
“/…”
会导致
“\\”
。很容易理解为什么后面的
/
被抑制;这些是名称分隔符,不能分隔任何内容。在我的Mac上,这两个字符的输出都是
“/…”
。这种行为显然依赖于操作系统。我怀疑在Windows上,前面的
“/”
表示计算机的网络路径,但计算机名不存在。

引发了什么异常?在许多文件系统上(包括Windows 10,虽然我没有测试过它),
/
目录没有父目录,因此资源路径
“/…”
无法解析为任何内容。@csmckelvey
NullPointer
异常。@TedHopp但是
/
应该转到
/out/
文件夹,就像在另一个不同的示例中一样,它使用
/
然后使用
应该从
/out/
移到一级,那么为什么要出现NullPointer异常?尝试使用
路径接口是Java.nio.file包的主要入口点之一。如果您的应用程序使用文件I/O,您将希望了解该类的强大功能。希望您能有更好的清晰度。
路径
类是
路径
引发什么异常的帮助类n?在许多文件系统上(我希望包括Windows 10,尽管我还没有测试过),
/
目录没有父目录,因此资源路径
“/…”
无法解析为任何内容。@csmckelvey
NullPointer
异常。@TedHopp但是
/
应该转到
/out/
文件夹,就像在另一个不同的示例中一样,它使用
/
然后使用
应该从
/out/
移到一级,那么为什么要出现NullPointer异常?尝试使用
路径接口是Java.nio.file包的主要入口点之一。如果您的应用程序使用文件I/O,您将希望了解该类的强大功能。希望您能有更好的清晰度。
Path
类是
Path
Perfect.和las的助手类