将相对路径转换为绝对路径

我有文件A的绝对路径

我有一个从文件A的目录到文件B的相对路径。此路径可以并且将使用“ ..”以任意复杂的方式向上移动目录结构。

范例A:

  • C:\projects\project1\module7\submodule5\fileA

示例B:

  • ..\..\module3\submodule9\subsubmodule32\fileB
  • ..\submodule5\fileB
  • ..\..\module7\..\module4\submodule1\fileB
  • fileB

我如何结合这两者以获得 绝对路径到文件B?

回答:

如果我的问题正确,那么您可以执行以下操作:

File a = new File("/some/abs/path");

File parentFolder = new File(a.getParent());

File b = new File(parentFolder, "../some/relative/path");

String absolute = b.getCanonicalPath(); // may throw IOException

以上是 将相对路径转换为绝对路径 的全部内容, 来源链接: utcz.com/qa/399964.html

回到顶部