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

Java 如何从文件中的相对路径构造文件,java,nio,Java,Nio,我正在分析此位置的基文件: /Users/haddad/development/fspc/content/2017.dev/src/agency/individual/integration/src/forms/print.xml 从这个文件中,我解析出: 。/../../../include/masking.xml 所以相对路径是从我解析出来的文件(基文件)的上下文中得到的 如何从该相对路径构造文件对象,以便读取其内容 因为您标记了nio,所以该类使这变得很容易。你只要打个电话就可以了 或:

我正在分析此位置的基文件:
/Users/haddad/development/fspc/content/2017.dev/src/agency/individual/integration/src/forms/print.xml

从这个文件中,我解析出:
。/../../../include/masking.xml

所以相对路径是从我解析出来的文件(基文件)的上下文中得到的


如何从该相对路径构造文件对象,以便读取其内容

因为您标记了
nio
,所以该类使这变得很容易。你只要打个电话就可以了

或:

输出

\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
注意:我在一个窗口机器上运行了这个,所以我当然得到了反斜杠


如果您更喜欢旧对象,可以使用双参数构造函数,并调用

输出

\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

由于您标记了
nio
,该类使这一过程变得简单。你只要打个电话就可以了

或:

输出

\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
注意:我在一个窗口机器上运行了这个,所以我当然得到了反斜杠


如果您更喜欢旧对象,可以使用双参数构造函数,并调用

输出

\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

您可以使用
子路径()
保留您感兴趣的路径部分,您可以与
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


您可以使用
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


你不能。您的引用路径只有3个父文件夹,而相对路径引用了4个父文件夹,因此它是无效的相对引用。您是否尝试了@Andreas,因为它是一个错误的父路径,我将其用于说明目的。将纠正this@MFisherKDX我认为路径有办法做到这一点。有兴趣看看如何从我的例子你不能。您的引用路径只有3个父文件夹,而相对路径引用了4个父文件夹,因此它是无效的相对引用。您是否尝试了@Andreas,因为它是一个错误的父路径,我将其用于说明目的。将纠正this@MFisherKDX我认为路径有办法做到这一点。有兴趣看看如何从我的例子可以做到这一点吗
System.out.println(new File(main));
System.out.println(new File(main, ref));
System.out.println(new File(main, ref).getCanonicalFile());
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);
}