简体   繁体   English

给定相对或绝对路径以及文件相对于的绝对路径时,如何获取文件的绝对路径

[英]How to get the absolute path of a file when given a relative or absolute path and the absolute path it is relative to

Let's say I have an absolute 'base' path: 假设我有一个绝对的“基本”路径:

/home/someone/dir1/dir2/

The user can pass me a new path, that can either be absolute or relative to base path, so the following would both be valid: 用户可以给我传递一个新路径,该路径可以是绝对路径,也可以相对于基本路径,因此以下两个路径均有效:

..
/home/someone/dir1/

How do I get java to give me the correct absolute path ie for both these cases: 我如何获得java给我正确的绝对路径,即这两种情况下:

/home/someone/dir1/

and do this in a platform-independent way? 并以与平台无关的方式做到这一点?

I tried the following: 我尝试了以下方法:

File resolvedFile = new File((new File(basePath).toURI().resolve(new File(newPath).toURI())));

However, where newPath was relative, newFile(newPath) resolves it automatically against the current working directory, rather than the basePath I want to supply. 但是,如果newPath是相对的,则newFile(newPath)会根据当前的工作目录而不是我要提供的basePath自动解析它。

Any thoughts? 有什么想法吗? Many thanks! 非常感谢!

Answering my own question.. 回答我自己的问题..

Seems like it can be done in java 7 using Path: 似乎可以使用Path在Java 7中完成:

Path p1 = Paths.get("/home/joe/foo");
// Result is /home/joe/foo/bar
System.out.format("%s%n", p1.resolve("bar"));

Since I can't get java 7 for my mac 10.5.8, I'm going with something like (NB NOT THOROUGHLY TESTED!): 由于我无法在Mac 10.5.8上安装Java 7,因此我将使用类似(注意:未进行彻底测试!):

static String getAbsolutePath(String basePath, String relativeOrAbsolutePath) throws IOException {

  boolean isAbsolute = false; File relativeOrAbsoluteFile = new File(relativeOrAbsolutePath); if (relativeOrAbsoluteFile.isAbsolute()){ isAbsolute = true; } if (isAbsolute){ return relativeOrAbsolutePath; } else { File absoluteFile = new File(basePath, relativeOrAbsolutePath); return absoluteFile.toString(); } } 

Take a look at File#getCanonicalPath 看看File#getCanonicalPath

From the JavaDocs: 从JavaDocs:

Returns the canonical pathname string of this abstract pathname. 返回此抽象路径名的规范路径名字符串。 A canonical pathname is both absolute and unique. 规范路径名是绝对的,也是唯一的。 The precise definition of canonical form is system-dependent. 规范形式的精确定义取决于系统。 This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. 如有必要,此方法首先将该路径名转换为绝对形式,就像通过调用getAbsolutePath()方法一样,然后以与系统有关的方式将其映射为其唯一形式。 This typically involves removing redundant names such as "." 这通常涉及删除多余的名称,例如“。”。 and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms). 和(..)从路径名,解析符号链接(在UNIX平台上),并将驱动器号转换为标准大小写(在Microsoft Windows平台上)。

Every pathname that denotes an existing file or directory has a unique canonical form. 表示现有文件或目录的每个路径名都具有唯一的规范形式。 Every pathname that denotes a nonexistent file or directory also has a unique canonical form. 表示不存在的文件或目录的每个路径名也具有唯一的规范形式。 The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. 创建文件或目录后,不存在的文件或目录的路径名的规范形式可能与相同路径名的规范形式不同。 Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted. 同样,在删除文件或目录后,现有文件或目录的路径名的规范形式可能与相同路径名的规范形式不同。

try this in your code. 在您的代码中尝试一下。

System.setProperty("user.dir", "your_base_path") System.setProperty(“ user.dir”,“ your_base_path”)

Not sure if this works outside of my setup (windows platform, JRE 1.6.x) 不知道这是否可以在我的设置之外运行(Windows平台,JRE 1.6.x)

but the following worked like a trick: 但是下面的工作就像一个把戏:

File path = new File(relativeOrAbsoluteGoldpath);
absolutePath = path.getCanonicalPath();

where relativeOrAbsoluteGoldpath is an arbitrary path name that may or may not be relative. 其中relativeOrAbsoluteGoldpath是可能是相对的或可能不是相对的任意路径名。

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

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