Search code examples
javapathnio

Convert a path to absolute path


I have a path object that needs to be converted to absolute path.

Path path = Paths.get("..\\this\\that\\blah.txt");

System.out.println(path.toFile().getCanonicalPath());

This is skipping the main project folder due to which I can't access the file. I want something like:

C:\Folder\ProjectFolder\this\\that\\blah.txt

instead of

C:\Folder\this\\that\\blah.txt

Solution

  • I have the feeling that @Brian Gordon is right and you just need to do:

    Path path = Paths.get("this\\that\\blah.txt");
    

    instead of:

    Path path = Paths.get("..\\this\\that\\blah.txt");
    

    .. represents the parent directory.
    So, if you're in C:\Folder\ProjectFolder, .. represents C:\Folder.