File io 如何使用ant脚本从文件中逐行读取数据?
在perl中,我们使用File io 如何使用ant脚本从文件中逐行读取数据?,file-io,ant,line-by-line,File Io,Ant,Line By Line,在perl中,我们使用从文件中逐行读取数据。如何使用ant脚本执行相同操作。您可以将任务与来自的任务结合使用(您必须下载并安装ant contrib) @{line} 只是我自己做的,实际上for+line.separator解决方案有缺陷,因为: 仅当文件EOL与平台EOL匹配时,它才起作用 它丢弃空行 以下是基于上一个示例的另一个(更好)解决方案: <project name="test" default="compile"> <taskdef resource
从文件中逐行读取数据。如何使用ant脚本执行相同操作。您可以将任务与来自的任务结合使用(您必须下载并安装ant contrib)
@{line}
只是我自己做的,实际上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>
@{line}
使用令牌的示例对我不起作用。在我的场景中,我只想打印一个自述文件,同时保留空白行。这就是我所做的
<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>
@{line}
试试这个,应该可以
<project name="test" default="compile">
<loadfile property="file" srcfile="Help.txt"/>
<target name="compile">
<echo>${file}</echo>
</target>
</project>
${file}
您能提供更多的上下文吗?您想做什么?请参阅:谢谢@martin在filterchain和head filter的帮助下,我能够从文件中读取数据。我无法在我的应用程序中安装ant contrib
。您能告诉我如何只读取文件的第一行,而不使用for
@rajaashok您可以使用insideloadfile
来解释文件的来源吗?我在ant文档中找不到它。它是用来按行拆分的吗?它是一个资源集合,请看这并没有回答问题
<project name="test" default="compile">
<loadfile property="file" srcfile="Help.txt"/>
<target name="compile">
<echo>${file}</echo>
</target>
</project>