Converting Relative Paths to Absolute Paths

前端 未结 9 1252
北荒
北荒 2020-12-05 04:14

I have an absolute path to file A.

I have a relative path to file B from file A\'s directory. This path may and will use \"..\" to go up the directory structure in

9条回答
  •  渐次进展
    2020-12-05 04:48

    In Java 7 you can also use the Path interface:

    Path basePath = FileSystems.getDefault().getPath("C:\\projects\\project1\\module7\\submodule5\\fileA");
    Path resolvedPath = basePath.getParent().resolve("..\\..\\module3\\submodule9\\subsubmodule32\\fileB"); // use getParent() if basePath is a file (not a directory) 
    Path abolutePath = resolvedPath.normalize();
    

提交回复
热议问题