Where to use resolve() and relativize() method of java.nio.file.Path class?

后端 未结 5 1638
忘了有多久
忘了有多久 2021-02-07 12:12
Path p1 = Paths.get(\"/Users/jack/Documents/text1.txt\");
Path p2 = Paths.get(\"/Users/jack/text2.txt\");
Path result1 = p1.resolve(p2);
Path result2 = p1.relativize(p2)         


        
5条回答
  •  轮回少年
    2021-02-07 12:40

    resolve(): Joins two paths.

    relativize (): construct a path from one location in the file system to another location.

    Output explanation:

    result1: /Users/jack/text2.txt: because you passed in an absolute path, resolve() returns the passed in the path as is if it is an absolute path.

    result2: ../../text2.txt: to get to /Users/jack/text2.txt from /Users/jack/Documents/text1.txt" you need to to go two levels up, and then just select `text2.txt file.

提交回复
热议问题