Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 661
rashok Avatar asked Mar 28 '11 06:03

rashok


2 Answers

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>
like image 53
Lesmana Avatar answered Nov 07 '22 23:11

Lesmana


Just had to do that myself, actually the for + line.separator solution is flawed because :

  • it only works if the file EOLs match the platform EOL
  • it discards empty lines

Here is another (better) solution based on the previous example :

<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>
like image 5
mat007 Avatar answered Nov 08 '22 00:11

mat007