简体   繁体   English

将路径转换为绝对路径

[英]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

Try to get the absolute path from where you call the file itself. 尝试从调用文件本身的位置获取绝对路径。

Example: 例:

    File blah = new File("/this/that/blah.txt");
    String blahAP = blah.getAbsolutePath();
    System.out.println("AP: " + blahAP);

This should work, but I have no computer around me at the moment to make sure. 这应该可以工作,但是请确保我目前没有电脑在身边。

I have the feeling that @Brian Gordon is right and you just need to do: 我觉得@Brian Gordon是正确的,您只需要这样做:

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 . 因此,如果您在C:\\Folder\\ProjectFolder ,则..表示C:\\Folder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM