如何获取“../../dir/file.ext”的完整真实文件路径在java中?
我在 java.io 包中有一种方法可以将包含“../”的相对路径转换为绝对路径吗?
我的目标是删除路径的“../”部分,因为
java.awt.Desktop.getDesktop().open()
Windows下的文件路径似乎不支持../
I there a way in the java.io package to convert a relative path containing "../" to an absolute path?
My goal is to remove the "../" part of the path because
java.awt.Desktop.getDesktop().open()
Does not seem to support ../ in file path under windows
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
--- 在评论 ../ 仍在路径中时进行编辑 ---
运行
将根据我当前的工作目录
/home/ebuck/workspace/State
输出您要求提供完整的真实文件路径,从技术上讲,绝对路径是完整的真实文件路径,它只是不是最短的完整真实文件路径。因此,如果您想快速而肮脏地完成此操作,只需将“../../home”添加到当前工作目录即可获取完整的文件路径(尽管这是一个包含不必要信息的冗长路径)。
如果您想要最短完整的文件路径,则可以使用
getCanonicalPath()
。它抛出异常;因为,当在根目录中时,一些小丑可能会要求“../../home”。--- 原始帖子如下,经过编辑 ---
将折叠(跟随)相对路径链接(
.
和..
)。将在不折叠(跟随)相对路径链接的情况下执行此操作。
--- Edited when comment was made that the ../ was still in the path ---
will run with the output
based on my current working directory of
/home/ebuck/workspace/State
Note that you asked for the complete real file path, which technically an absolute path is a complete, real file path, it's just not the shortest complete real file path. So, if you want to do it fast and dirty, one can just add "../../home" to the current working directory and obtain a full, complete file path (albeit a wordy one that contains unnecessary information).
If you want the shortest full, complete file path, that's what
getCanonicalPath()
is used for. It throws an exception; because, some joker out there will probably ask for "../../home" when in the root directory.--- Original post follows, with edits ---
Will do so collapsing (following) the relative path links (
.
and..
).Will do so without collapsing (following) the relative path links.
有时 File.getCanonicalPath() 可能不需要,因为它可能会解析符号链接之类的东西,因此如果您想维护 File.getAbsolutePath() 提供的“逻辑”路径,则不能使用 getCanonicalPath()。此外,IIRC、gCP() 可以引发异常,而 gAP() 则不会,并且 gAP() 可以引用不存在的路径。
我很久以前就遇到过“..”问题。这是我编写的用于删除路径中的“..”的实用方法:
Sometimes File.getCanonicalPath() may not be desired since it may resolve things like symlinks, so if you want to maintain the "logical" path that File.getAbsolutePath() provides, you cannot use getCanonicalPath(). Also, IIRC, gCP() can thrown an exception while gAP() does not, and gAP() can refer to a path does not exist.
I've encounter the '..' problem a long time ago. Here is a utility method I wrote to remove the '..'s in a path: