如何使用ant脚本从文件中逐行读取数据?

ras*_*hok 13 ant file-io line-by-line

在perl中,我们用来<FileDescriptor>从文件中读取数据.如何使用ant脚本执行相同操作.

les*_*ana 28

您可以loadfile将该for任务与ant-contrib中的任务结合使用(您必须下载并安装ant-contrib).

<project name="test" default="compile">

  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
      <pathelement location="path/to/ant-contrib.jar"/>
    </classpath>
  </taskdef>

  <loadfile property="file" srcfile="somefile.txt"/>

  <target name="compile">
    <for param="line" list="${file}" delimiter="${line.separator}">
      <sequential>
        <echo>@{line}</echo>
      </sequential>
    </for>
  </target>

</project>
Run Code Online (Sandbox Code Playgroud)


mat*_*007 5

自己要做,实际上for + line.separator解决方案存在缺陷,因为:

  • 仅在文件EOL与平台EOL匹配时才有效
  • 它丢弃空行

这是基于前面的示例的另一个(更好的)解决方案:

<project name="test" default="compile">

  <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
      <pathelement location="path/to/ant-contrib.jar"/>
    </classpath>
  </taskdef>

  <loadfile property="file" srcfile="somefile.txt"/>

  <target name="compile">
    <for param="line">
      <tokens>
        <file file="${file}"/>
      </tokens>
      <sequential>
        <echo>@{line}</echo>
      </sequential>
    </for>
  </target>

</project>
Run Code Online (Sandbox Code Playgroud)