We must be able to construct Path objects by adding and subtracting pieces to our Path. To subtract the base of a Path we use relativize() and to add pieces at the end of a Path we use resolve() (not exactly “discoverable” names).
// files/AddAndSubtractPaths.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.io.IOException;
import java.nio.file.*;
public class AddAndSubtractPaths {
static Path base = Paths.get("..", "..", "..").toAbsolutePath().normalize();
static void show(int id, Path result) {
if (result.isAbsolute()) {
System.out.println("(" + id + ") " + base.relativize(result));
} else {
System.out.println("(" + id + ") " + result);
}
try {
System.out.println("RealPath: " + result.toRealPath());
} catch (IOException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
System.out.println(System.getProperty("os.name"));
System.out.println(base);
Path p = Paths.get("AddAndSubtractPaths.java").toAbsolutePath();
show(1, p);
Path convoluted =
p.getParent()
.getParent()
.resolve("strings")
.resolve("..")
.resolve(p.getParent().getFileName());
System.out.println(
"convoluted ..:" + p.getParent().getParent().resolve("strings").resolve(".."));
show(2, convoluted);
show(3, convoluted.normalize());
Path p2 = Paths.get("..", "..");
show(4, p2);
show(5, p2.normalize());
show(6, p2.toAbsolutePath().normalize());
Path p3 = Paths.get(".").toAbsolutePath();
Path p4 = p3.resolve(p2);
show(7, p4);
show(8, p4.normalize());
Path p5 = Paths.get("").toAbsolutePath();
show(9, p5);
show(10, p5.resolveSibling("strings"));
show(11, Paths.get("nonexistent"));
}
}
/* My Output:
Mac OS X
/Users/wangbingfeng
(1) github/OnJava8-Examples/files/AddAndSubtractPaths.java
RealPath: /Users/wangbingfeng/github/OnJava8-Examples/files/AddAndSubtractPaths.java
convoluted:/Users/wangbingfeng/github/OnJava8-Examples/strings
convoluted ..:/Users/wangbingfeng/github/OnJava8-Examples/strings/..
(2) github/OnJava8-Examples/strings/../files
RealPath: /Users/wangbingfeng/github/OnJava8-Examples/files
(3) github/OnJava8-Examples/files
RealPath: /Users/wangbingfeng/github/OnJava8-Examples/files
(4) ../..
RealPath: /Users/wangbingfeng/github
(5) ../..
RealPath: /Users/wangbingfeng/github
(6) github
RealPath: /Users/wangbingfeng/github
(7) github/OnJava8-Examples/files/./../..
RealPath: /Users/wangbingfeng/github
(8) github
RealPath: /Users/wangbingfeng/github
(9) github/OnJava8-Examples/files
RealPath: /Users/wangbingfeng/github/OnJava8-Examples/files
(10) github/OnJava8-Examples/strings
RealPath: /Users/wangbingfeng/github/OnJava8-Examples/strings
(11) nonexistent
java.nio.file.NoSuchFileException: nonexistent
*/
resolve
Path resolve(String other)
Converts a given path string to a Path and resolves it against this Path in exactly the manner specified by the resolve method. For example, suppose that the name separator is "/" and a path represents "foo/bar", then invoking this method with the path string "gus" will result in the Path "foo/bar/gus".
Parameters:
other - the path string to resolve against this path
Returns:
the resulting path
Throws:
InvalidPathException - if the path string cannot be converted to a Path.
See Also:
FileSystem.getPath(java.lang.String, java.lang.String...)
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/files/AddAndSubtractPaths.java
3. https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#resolveSibling-java.lang.String-
本文深入探讨了Java中Path对象的创建、添加与减法操作,通过具体实例展示了如何使用relativize()和resolve()方法来处理文件路径。文章涵盖了路径规范化、绝对路径转换及解决复杂路径表达式等内容。
2165

被折叠的 条评论
为什么被折叠?



