要从中消除多余的元素,Path我们可以使用Path.normalize()方法。例如下面的代码片段。当尝试访问README当前目录中的文件时,元素中的.符号Path被认为是多余的,我们不需要它。这就是为什么我们将规范化Path。
package org.nhooo.example.io;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathNormalize {
public static void main(String[] args) {
//以下路径包含一个冗余元素。“。” 哪一个"." which
// 基本上指向当前目录时可以简单地删除
// 我们正在使用当前目录。
Path path = Paths.get("./README");
System.out.println("Path = " + path);
// 从路径中删除多余的名称元素。
path = path.normalize();
System.out.println("Path = " + path);
}
}