我只需要知道如何修改下面的代码,以便蚂蚁确定我的文本文件中的行数(请注意这里的所有文本文件将具有相同的行数,但该数字不固定)并自动执行它们基于循环。
<project name="ant-read-n-files" default="run" basedir=".">
<!-- Load the ant contrib lib -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${basedir}/lib/ant-contrib.jar"/>
</classpath>
</taskdef>
<target name="read">
<!-- file a -->
<loadfile property="textFileA" srcfile="${basedir}/files/aaa.txt">
<filterchain>
<headfilter lines="1" skip="${linenum}"/>
</filterchain>
</loadfile>
<for param="line" list="${textFileA}" delimiter="${line.separator}">
<sequential>
<property name="textFileAValue" value="@{line}"/>
</sequential>
</for>
<!-- file b -->
<loadfile property="textFileB" srcfile="${basedir}/files/bbb.txt">
<filterchain>
<headfilter lines="1" skip="${linenum}"/>
</filterchain>
</loadfile>
<for param="line" list="${textFileB}" delimiter="${line.separator}">
<sequential>
<property name="textFileBValue" value="@{line}"/>
</sequential>
</for>
<!-- file c -->
<loadfile property="textFileC" srcfile="${basedir}/files/ccc.txt">
<filterchain>
<headfilter lines="1" skip="${linenum}"/>
</filterchain>
</loadfile>
<for param="line" list="${textFileC}" delimiter="${line.separator}">
<sequential>
<property name="textFileCValue" value="@{line}"/>
</sequential>
</for>
<!-- Print them all -->
<echo message="${textFileAValue}"/>
<echo message="${textFileBValue}"/>
<echo message="${textFileCValue}"/>
</target>
<target name="run">
<foreach param="linenum" list="0,1,2" target="read"/>
</target>
</project>
&#13;
在这里你看到list =&#34; 0,1,2&#34;意味着循环将验证每个文本文件的前三行,但我希望它是动态的,具体取决于文件所具有的行数。 非常感谢任何帮助。
谢谢, 阿什利
答案 0 :(得分:1)
以下解决方案适用于此: -
<project name="ant-read-n-files" default="run" basedir=".">
<!-- Load the ant contrib lib -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${basedir}/lib/ant-contrib.jar"/>
</classpath>
</taskdef>
<target name="read">
<!-- file a -->
<loadfile property="textFileA" srcfile="${basedir}/files/aaa.txt">
<filterchain>
<headfilter lines="1" skip="${linenum}"/>
</filterchain>
</loadfile>
<for param="line" list="${textFileA}" delimiter="${line.separator}">
<sequential>
<property name="textFileAValue" value="@{line}"/>
</sequential>
</for>
<!-- file b -->
<loadfile property="textFileB" srcfile="${basedir}/files/bbb.txt">
<filterchain>
<headfilter lines="1" skip="${linenum}"/>
</filterchain>
</loadfile>
<for param="line" list="${textFileB}" delimiter="${line.separator}">
<sequential>
<property name="textFileBValue" value="@{line}"/>
</sequential>
</for>
<!-- file c -->
<loadfile property="textFileC" srcfile="${basedir}/files/ccc.txt">
<filterchain>
<headfilter lines="1" skip="${linenum}"/>
</filterchain>
</loadfile>
<for param="line" list="${textFileC}" delimiter="${line.separator}">
<sequential>
<property name="textFileCValue" value="@{line}"/>
</sequential>
</for>
<!-- Print them all -->
<echo message="${textFileAValue}"/>
<echo message="${textFileBValue}"/>
<echo message="${textFileCValue}"/>
</target>
<target name="run">
<!-- Get number of lines of one of the files -->
<loadfile property="textFile" srcfile="${basedir}/files/aaa.txt"/>
<resourcecount property="line.count" count="0" when="eq">
<tokens>
<concat>
<filterchain>
<tokenfilter>
<stringtokenizer delims="${line.separator}" />
</tokenfilter>
</filterchain>
<propertyresource name="textFile" />
</concat>
</tokens>
</resourcecount>
<echo message="Number of lines: ${line.count}" />
<script language="javascript">
var list="", n=parseInt(project.getProperty("line.count"), 0);
for (var i = 0; i < n; i++) list += i + ",";
project.setProperty("list", list);
</script>
<foreach param="linenum" list="${list}" target="read"/>
</target>
</project>
&#13;