Understanding java.nio.file.Path.relativize (Another way)

I am trying to get acquainted with java.nio.file.Path.relativize () to no avail.

I read javadocs and I saw examples. However, I still cannot figure out the following example (I use Linux, I apologize to window users):

The working directory for the program: / home / userspace / workspace / java8.

With two files: /home/userspace/workspace/java8/zoo.txt and /home/userspace/temp/delete/dictionary.txt

The following program calls Path.relativize ():

package certExam.java8.ch9NIO.paths;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Relativize 
{
    public static void main(String[] args) 
    {
        Path relativePathToZoo = Paths.get("zoo.txt");
        Path relativePathToDictionary = Paths.get("../../temp/delete/dictionary.txt");
        System.out.println("relativePathToZoo.relativize(relativePathToDictionary): "+relativePathToZoo.relativize(relativePathToDictionary));
        System.out.println("relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath(): "+relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath());
        System.out.println("relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath().normalize(): "+relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath().normalize());
        System.out.println("relativePathToDictionary.relativize(relativePathToZoo): "+relativePathToDictionary.relativize(relativePathToZoo));
        System.out.println("relativePathToDictionary.relativize(relativePathToZoo).toAbsolutePath().normalize(): "+relativePathToDictionary.relativize(relativePathToZoo).toAbsolutePath().normalize());
        System.out.println();
    }
}

Conclusion:

relativePathToZoo.relativize(relativePathToDictionary): ../../../temp/delete/dictionary.txt 
    relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath(): /home/userspace/workspace/java8/../../../temp/delete/dictionary.txt 
    relativePathToZoo.relativize(relativePathToDictionary).toAbsolutePath().normalize(): /home/temp/delete/dictionary.txt 
    relativePathToDictionary.relativize(relativePathToZoo): ../../../../../zoo.txt 
    relativePathToDictionary.relativize(relativePathToZoo).toAbsolutePath().normalize(): /zoo.txt

My question, a bit that I cannot understand, is why is the output of relativePathToDictionary.relativize (relativePathToZoo) ../../../../../ zoo.txt?

Under normalization, this will make you think that zoo.txt lives in the root directory.

() ? , relativize() , .. . , zoo.txt dictionary.txt.

,

+4
2

, . Windows, , \\ /.

relativize? , foo bar baz, foo bar hello, .. hello, , foo bar baz, foo bar hello, .. Paths.get("foo", "bar", "baz").resolve(Paths.get("..", "hello")).normalize() , Paths.get("foo", "bar", "hello"), .

JDK-6925169, Berger . Path . .. relativize, .

, Paths.get("..", "..", "temp", "delete", "dictionary.txt") Paths.get("a", "b", "c", "d", "e"), , , , Paths.get("zoo.txt"). Windows, Linux. :

Path relative = Paths.get("zoo.txt");
Path base1 = Paths.get("..", "..", "temp", "delete", "dictionary.txt");
Path base2 = Paths.get("a",  "b",  "c",    "d",      "e");

Path relativized1 = base1.relativize(relative);
System.out.println("relativized1: "+relativized1);
Path relativized2 = base2.relativize(relative);
System.out.println("relativized2: "+relativized2);

Path resolved1 = base1.resolve(relativized1).normalize();
System.out.println("resolved1="+resolved1);
Path resolved2 = base2.resolve(relativized2).normalize();
System.out.println("resolved2="+resolved2);

relatize , , normalize .., , zoo.txt.

, , dictionary.txt, . relativize , resolve, "... ".

+9

zoo.txt elephant.bin . zoo.txt , zoo.txt elephant.bin. , ".." temp. , ! java, "..". . bin java, "/bin". "/elephant.bin". .

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); 

, ,

../../bin/elephant.bin
+9

All Articles