Understanding java.nio.file.Path.relativize(Path other)

前端 未结 2 1144

I am trying to familiarize myself with java.nio.file.Path.relativize() to no avail.

I have read the javadocs, and I have seen examples. However, I still cannot get my

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 06:19

    We want to get from zoo.txt to elephant.bin in this example. So let's start at zoo.txt and ask ourselves how do I get from zoo.txt to elephant.bin. First I have to go up a directory so I use ".." Now I'm in temp. Trace the steps with you finger if it helps! I have to go up one more to java so I use ".." again. Now I'm in java. The file bin is in java so I go down to it using "/bin". Once more I go down using "/elephant.bin". We have arrived at our destination.

    Path p1 = Paths.get("java/temp/zoo.txt");
    Path p2 = Paths.get("java/bin/elephant.bin");
    
    Path p1Top2 = p1.relativize(p2);
    System.out.println(p1Top2); 
    

    Put all of the above steps we took together and you get the output

    ../../bin/elephant.bin
    

提交回复
热议问题