How to read data line by line from a file using ant script?

rashok picture rashok · Mar 28, 2011 · Viewed 38.3k times · Source

In perl we use <FileDescriptor> to read data line by ilne from a file. How to do the same using ant script.

Answer

lesmana picture lesmana · Mar 28, 2011

You can do that using the loadfile task in combination with the for task from ant-contrib (you will have to download and install 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>