Java URI 简单路径

Java URI simple path

我刚刚在 JAVA 上发现了 URI 方法。

但我只是想让这个方法向我展示一个字符串的字符串的简化路径。

问题出在以下代码中:

public static void main (String [] args) throws Exception {
    URI uri = new URI("/a/./b/../../c/");
    System.out.println("Path =" + uri.getPath());
}

当我执行代码时,它向我发送了整个字符串而不是“/ c”,这是我等待的结果。

提前感谢您的澄清:)

根据JavaDocs,

Normalization is the process of removing unnecessary "." and ".." segments from the path component of a hierarchical URI. Each "." segment is simply removed. A ".." segment is removed only if it is preceded by a non-".." segment. Normalization has no effect upon opaque URIs.

URI class 的 normalize() 方法应该可以满足您的需求。