在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. 用bash腳本逐行讀取文件
- 4. 如何使用SQLite腳本逐行讀取.txt文件?
- 5. 從使用shell腳本的文件逐行讀取
- 6. 如何從bash腳本中的文件逐行讀取?
- 7. 如何逐行讀取文本文件?
- 8. 如何逐行讀取文本文件?
- 9. 從文本文件逐行讀取C++
- 10. android - 從文本文件逐行讀取
- 11. 從文本文件中逐行讀取數據
- 12. 用jQuery逐行讀取文本文件
- 13. 如何從數據庫中讀取txt文件(逐行)
- 14. 如何在VB腳本中逐行讀取文件?
- 15. 如何在Bash腳本中逐行讀取文件?
- 16. 如何逐行讀取uitextview文本行?
- 17. 從文件中逐行讀取整數
- 18. 如何從vb 6中逐行讀取文本文件內容?
- 19. 如何從FTP服務器上逐行讀取文本文件?
- 20. 逐行讀取文本文件
- 21. 從文件行逐行讀取
- 22. 使用for..in逐行讀取文件
- 23. 使用python逐行讀取pdf文件
- 24. 如何從txt文件中逐字符讀取字符並逐行讀取C++
- 25. 如何逐行讀取NSFileHandle的數據?
- 26. 如何逐行讀取大文件
- 27. 如何獲取文件逐行閱讀
- 28. 如何逐行讀取gzip文件?
- 29. 在java中從文本逐行讀取數據
- 30. 雲數據流:讀取整個文本文件而不是逐行讀取
你能給更多的環境?你想做什麼? – 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