【问题标题】:How do generate a directory path with double-backslash or forward-slash separators?如何生成带有双反斜杠或正斜杠分隔符的目录路径?
【发布时间】:2010-10-05 13:58:47
【问题描述】:

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

在我的 ant 脚本中,我有:

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

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

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

trainer.dir=../trainer

最后写成:

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

到 commonConfig.properties 文件。

我需要它来写:

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

或者,我需要它来写:

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

我该怎么做?

【问题讨论】:

    标签: java windows unix ant


    【解决方案1】:

    这看起来很像这个问题:Ant produces jsfl with backslashes instead of slashes

    所以,尝试使用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"/>
    

    【讨论】:

    • 您是如何将源代码格式添加到我的帖子中的?我似乎无法弄清楚。
    • 我认为它看起来不错 - 当您在降价编辑器中时,您应该能够使用上面的按钮行,允许您将格式应用于编辑框中的选定文本。有关一些信息,请参阅stackoverflow.com/editing-help。我希望能够做一个小的编辑来摆脱水平滚动条,但不想改变太多。我在第二个回显中添加了append 属性 - 可能不是必需的,但没有它,文件将被第二个回显截断。
    • 啊,感谢您的提示和修复。我从不使用 echo 写入文本文件,这样错误就会潜入。
    猜你喜欢
    • 2013-07-23
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 2021-05-21
    • 2020-11-20
    • 1970-01-01
    相关资源
    最近更新 更多