开发者

Why new File("c:\\a") not equals new File("c:\\a","\\")?

开发者 https://www.devze.com 2023-03-31 12:08 出处:网络
the开发者_如何学Gose equal to each other: new File(\"c:\\\\a\") new File(\"c:\\\\a\\\\\") new File(\"c:\\\\a\",\"\")

the开发者_如何学Gose equal to each other:

new File("c:\\a")
new File("c:\\a\\")
new File("c:\\a","")
new File("c:\\a\\","")

but new File("c:\\a","\\") not equals to them

but new File("c:\\a","\\b") equals to new File("c:\\a","b")

why?


The JavaDocs for java.io.File should make this clear.

File(File parent, String child)
          Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
          Creates a new File instance by converting the given pathname string into an abstract pathname.


equals() - Compares two abstract pathnames lexicographically.

The following prints make it clear, that this have not the same path.

System.out.println(new File("c:\\a").getAbsolutePath());
System.out.println(new File("c:\\a\\").getAbsolutePath());
System.out.println(new File("c:\\a","").getAbsolutePath());
System.out.println(new File("c:\\a\\","").getAbsolutePath());
System.out.println(new File("c:\\a","\\").getAbsolutePath());
System.out.println(new File("c:\\a","\\b").getAbsolutePath());
System.out.println(new File("c:\\a","b").getAbsolutePath());

output

c:\a
c:\a
c:\a
c:\a
c:\a\
c:\a\b
c:\a\b


I know nothing about Java, but isnt it because "c:\a" is a file, and "\" is a directory?

0

精彩评论

暂无评论...
验证码 换一张
取 消