windows 如何生成带有双反斜杠或正斜杠分隔符的目录路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3864326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:22:07  来源:igfitidea点击:

How do generate a directory path with double-backslash or forward-slash separators?

javawindowsunixant

提问by mainstringargs

I am writing a directory path to a text file from ant, which is later read by a Java Application to find another file.

我正在从 ant 写入一个文本文件的目录路径,稍后由 Java 应用程序读取以查找另一个文件。

In my ant script I have:

在我的蚂蚁脚本中,我有:

<property name="fulltrainer.dir"  location="${trainer.dir}" />

<echo file="${trainer.dir}/properties/commonConfig.properties"># KEY         VALUE
CurrentBuildFile=${fulltrainer.dir}\current_build</echo>

in the build.properties file trainer.dir is set to:

在 build.properties 文件中 trainer.dir 设置为:

trainer.dir=../trainer

It ends up writing:

它最终写成:

# KEY        VALUE
CurrentBuildFile=C:\Workspaces\ralph\trainer\current_build

to the commonConfig.properties file.

到 commonConfig.properties 文件。

I need it to write:

我需要它写:

# KEY        VALUE
CurrentBuildFile=C:\Workspaces\ralph\trainer\current_build

or, I need it to write:

或者,我需要它写:

# KEY        VALUE
CurrentBuildFile=C:/Workspaces/ralph/trainer/current_build

How can I do that?

我怎样才能做到这一点?

回答by Andy

This looks a lot like this question: Ant produces jsfl with backslashes instead of slashes

这看起来很像这个问题:Ant生成带有反斜杠而不是斜杠的jsfl

So, try using the pathconverttask.

因此,请尝试使用该pathconvert任务。

<pathconvert targetos="unix" property="fulltrainer.unix_dir">
    <path location="${trainer.dir}"/>
</pathconvert>

<property name="cf.props" value="${trainer.dir}/properties/commonConfig.properties"/>
<echo file="${cf.props}" message="# KEY         VALUE"/>
<echo file="${cf.props}" append="yes" message="CurrentBuildFile=${fulltrainer.unix_dir}/current_build"/>