Как читать данные по строкам из файла с помощью ant script?

В perl мы используем <FileDescriptor> для чтения строки данных ilne из файла. Как сделать то же самое с помощью ant script.

Ответы

Ответ 1

Вы можете сделать это, используя задачу 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>

Ответ 2

Просто нужно было сделать это сам, на самом деле решение 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>

Ответ 3

Пример использования токенов не работал у меня. В моем сценарии я пытался просто распечатать файл README, сохраняя пустые строки. Вот что я сделал.

<taskdef name="if-contrib" classname="net.sf.antcontrib.logic.IfTask" classpath="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />
<taskdef name="for-contrib" classname="net.sf.antcontrib.logic.ForTask" classpath="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />
<taskdef name="var-contrib" classname="net.sf.antcontrib.property.Variable" classpath="${basedir}/lib/ant/ant-contrib-1.0b3.jar" />
<target name="help">
    <for-contrib param="line">
        <tokens>
            <file file="README.txt" />
        </tokens>
        <sequential>
            <var-contrib name="line.length" unset="true" />
            <length string="@{line}" property="line.length" />
            <if-contrib>
                <equals arg1="${line.length}" arg2="0" />
                <then>
                    <echo>
                    </echo>
                </then>
                <else>
                    <echo>@{line}</echo>
                </else>
            </if-contrib>
        </sequential>
    </for-contrib>
</target>

Ответ 4

Попробуйте Это должно быть работа.....

<project name="test" default="compile">
 <loadfile property="file" srcfile="Help.txt"/>
   <target name="compile">
    <echo>${file}</echo> 
   </target>
</project>