在perl中,我们使用<FileDescriptor>
来从文件中读取ilne的数据行。如何使用ant脚本来做同样的事情。如何使用ant脚本从文件逐行读取数据?
13
A
回答
27
您可以使用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>
4
就必须这样做我自己,实际上是为+ line.separator的解决方案是有缺陷的,因为:
- 它只能如果文件EOLS匹配平台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>
2
使用令牌的示例对我无效。在我的方案中,我只是在保留空白行的同时打印一个自述文件。这是我做的。
<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>
0
试试这应该是工作.....
<project name="test" default="compile">
<loadfile property="file" srcfile="Help.txt"/>
<target name="compile">
<echo>${file}</echo>
</target>
</project>
相关问题
- 1. 从shell脚本逐行读取文件
- 2. 如何使用NIO从gzip文本文件逐行读取文本数据?
- 3. 如何使用SQLite脚本逐行读取.txt文件?
- 4. 如何从bash脚本中的文件逐行读取?
- 5. 如何逐行读取文本文件?
- 6. 如何逐行读取文本文件?
- 7. android - 从文本文件逐行读取
- 8. 从文本文件中逐行读取数据
- 9. 用jQuery逐行读取文本文件
- 10. 如何从数据库中读取txt文件(逐行)
- 11. 如何在VB脚本中逐行读取文件?
- 12. 如何在Bash脚本中逐行读取文件?
- 13. 从文件中逐行读取整数
- 14. 从文件行逐行读取
- 15. 使用python逐行读取pdf文件
- 16. 如何逐行读取NSFileHandle的数据?
- 17. 如何逐行读取gzip文件?
- 18. 如何获取文件逐行阅读
- 19. 如何使用Java从文本文件读取奇数行?
- 20. shell脚本不逐行读取
- 21. 逐行读取csv文件
- 22. C++逐行读取文件
- 23. 从一个文件中逐行读取
- 24. C++从一个文件(包含空格)逐行读取数据
- 25. 使用java eclipse逐行读取文本文件
- 26. 逐行读取文件,逐字符C
- 27. 使用循环从多行文本文件中读取数据
- 28. 使用java从文本文件中读取数据行
- 29. 从ant脚本执行java类文件
- 30. 使用ANT将多个文本文件逐行合并
你能给更多的环境?你想做什么? – 2011-03-28 09:17:25
请参阅:http://ant.apache.org/manual/Types/filterchain.html#headfilter – 2011-03-28 13:48:29
感谢@martin与筛选器和头过滤器的帮助,我能够从文件中读取数据。 – rashok 2011-03-30 18:55:56