将相对路径转换为绝对路径 [英] Converting Relative Paths to Absolute Paths
本文介绍了将相对路径转换为绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文件A的绝对路径。
I have an absolute path to file A.
我有一个从文件A的目录到文件B的相对路径。此路径可能并将使用..以任意复杂的方式上升目录结构。
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 arbitrarily complex ways.
示例A:
-
C:\projects\project1 \module7 \ submodule5 \ fileA
C:\projects\project1\module7\submodule5\fileA
示例Bs:
-
.. \。 .\module3 \ submodule9 \subsubmodule32 \ fileB
-
.. \submodule5 \ fileB
-
.. \..\module7 \..\module4 \submodule1 \ fileB
-
fileB
..\..\module3\submodule9\subsubmodule32\fileB
..\submodule5\fileB
..\..\module7\..\module4\submodule1\fileB
fileB
如何组合这两个是为了获得文件B的最简单的绝对路径?
How do I combine the two in order to get the simplest possible absolute path to file B?
推荐答案
如果我得到你的问题是对的,你可以这样做:
If I get your problem right, you could do something like this:
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
这篇关于将相对路径转换为绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文