NOTE

Path

What Path is, absolute paths, and relative paths.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

What It Is

Represents a file path, similar to the File class.

Absolute Path

    // Linux
    Path path = Paths.get("<USER_HOME>/code/Java/my_spring_boot_template/pom.xml");
    // Windows
    Path path = Paths.get("c:\\data\\myfile.txt");
    Path path = Paths.get("c:/data/myfile.txt");

Relative Path

    Path path = Paths.get("<USER_HOME>/code/Java/my_spring_boot_template", "pom.xml");

    Path currentDir = Paths.get(".");

    Path parentDir = Paths.get("..");


    String originalPath = "<USER_HOME>/code/Java/my_spring_boot_template/../";

    Path path1 = Paths.get(originalPath);
    System.out.println("path1 = " + path1);

    Path path2 = path1.normalize();// Print the complete path
    System.out.println("path2 = " + path2);

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub