简体   繁体   English

如何从文件中的相对路径构造文件

[英]How to construct a file from a relative path in a File

I'm parsing a base file at this location: /Users/haddad/development/fspc/content/2017.dev/src/agency/individual/integration/src/forms/print.xml 我正在以下位置解析基础文件: /Users/haddad/development/fspc/content/2017.dev/src/agency/individual/integration/src/forms/print.xml

And from that file, I parse out: ../../../../include/masking.xml 然后从该文件中解析出: ../../../../include/masking.xml

So that relative path is from the context from the file I parsed it out from (base file) 因此,相对路径来自我从(基础文件)解析出来的文件的上下文

How can I constuct a file object from that relative path so I can read it's contents? 如何从该相对路径构造文件对象,以便可以读取其内容?

Since you tagged nio , the Path class makes this easy. 由于您标记了nio ,因此Path类使此操作变得容易。 You simply call resolveSibling() and normalize() . 您只需调用resolveSibling()normalize()

String main = "/Users/haddad/development/fspc/content/2017.dev/src/agency/individual/integration/src/forms/print.xml";
String ref = "../../../../include/masking.xml";

System.out.println(Paths.get(main));
System.out.println(Paths.get(main).resolveSibling(ref));
System.out.println(Paths.get(main).resolveSibling(ref).normalize());

Or: 要么:

System.out.println(Paths.get(main));
System.out.println(Paths.get(main, ref));
System.out.println(Paths.get(main, ref).normalize());

Output 产量

\Users\haddad\development\fspc\content\2017.dev\src\agency\individual\integration\src\forms\print.xml
\Users\haddad\development\fspc\content\2017.dev\src\agency\individual\integration\src\forms\..\..\..\..\include\masking.xml
\Users\haddad\development\fspc\content\2017.dev\src\agency\include\masking.xml

Note: I ran this on a Window machine, so I of course got backslashes 注意:我在Windows机器上运行了它,所以我当然得到了反斜杠


If you prefer the old File object, you use the two-argument constructor, and call getCanonicalFile() . 如果您更喜欢旧的File对象,则可以使用两个参数的构造函数,然后调用getCanonicalFile()

System.out.println(new File(main));
System.out.println(new File(main, ref));
System.out.println(new File(main, ref).getCanonicalFile());

Output 产量

\Users\haddad\development\fspc\content\2017.dev\src\agency\individual\integration\src\forms\print.xml
\Users\haddad\development\fspc\content\2017.dev\src\agency\individual\integration\src\forms\print.xml\..\..\..\..\include\masking.xml
C:\Users\haddad\development\fspc\content\2017.dev\src\agency\individual\include\masking.xml

You could use subpath() to keep the path part that interests you that you can combine with resolve() to append a new path to : 您可以使用subpath()保留您感兴趣的路径部分,您可以将其与resolve()结合使用,以将新路径附加到:

public static void main(String[] args) {

    Path printXmlPath = Paths.get("/Users/haddad/development/fspc/content/2017.dev/src/agency/individual/integration/src/forms/print.xml");

    Path maskingXmlPath = printXmlPath.subpath(0, printXmlPath.getNameCount() - 5)
                                      .resolve("include/masking.xml");

    System.out.println(maskingXmlPath);
}

Users\\haddad\\development\\fspc\\content\\2017.dev\\src\\agency\\include\\masking.xml 用户\\ haddad \\ development \\ fspc \\ content \\ 2017.dev \\ src \\ agency \\ include \\ masking.xml

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

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