Alan Zhang
Alan Zhang

Reputation: 15

About Path normalize() in Java

For the following code, the aPath turns out to be D:\ OCPJP7\ NIO2\ src\SubPath.java. Why is the programs element gone?

Path aPath = Paths.get(" D:\\ OCPJP7\\ programs\\..\\ NIO2\\ src\\.\\ SubPath.java"); 
aPath = aPath.normalize();

Upvotes: 1

Views: 1154

Answers (2)

Mena
Mena

Reputation: 48404

Because the normalization replaced the .. programs' parent directory.

So you have NIO2 as a sub-folder of OCPJP7.

Similarly, . goes away as it's redundant (indicates current directory within context).

Upvotes: 1

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

Because the programs element is followed by \\..\\ which means "to go up one directory level". This sequence removes the \\programs\\ part from your path.

Upvotes: 1

Related Questions