thomas.mc.work
thomas.mc.work

Reputation: 6474

java.nio.file.path: inconsistent behaviour with name count?

While learning about the behaviour of the NIO2 API I have considered this:

Path unix = Paths.get("/");
Path windows = Paths.get("c:\\");
System.out.println(unix.getNameCount());
System.out.println(windows.getNameCount());

... gives the output

0
1

Why is that? I would expect the same result which should actually be 0 because there is no name but only a root. When I add a folder

Path unix = Paths.get("/etc");
Path windows = Paths.get("c:\\etc");
System.out.println(unix.getNameCount());
System.out.println(windows.getNameCount());

... then I get

1
1

Isn't that confusing for the Windows part?

Edit: I'm on a linux machine myself.

Upvotes: 1

Views: 254

Answers (1)

thomas.mc.work
thomas.mc.work

Reputation: 6474

Alright, now I found the right explanation:

A Path instance reflects the underlying platform. In the Solaris OS, a Path uses the Solaris syntax (/home/joe/foo) and in Microsoft Windows, a Path uses the Windows syntax (C:\home\joe\foo). A Path is not system independent.

From here: http://docs.oracle.com/javase/tutorial/essential/io/pathClass.html

That means in my case on a linux machine the path "c:\\\\" would be the name of a relative folder within my working directory.

Upvotes: 2

Related Questions