Search code examples
anttask

How to read just a first line of an file.txt with ANT?


I would just read the first line of an file.txt with ANT without using externals libs.

my file is file.txt and his content :

3432424
43545435
756767
345354345

Solution

  • You can use loadfile ant task to load the file and HeadFilter in filterchain to filter out only the first line of loaded text.

    If your text file e.g. test.txt contains the following

    abc
    def
    ghi
    

    then the the target given bellow will print only the first line abc on the console

    <target name="read">
        <loadfile property="temp" srcfile="./test.txt">
            <filterchain>
                <filterreader classname="org.apache.tools.ant.filters.HeadFilter">
                    <param name="lines" value="1" />
                </filterreader>
            </filterchain>
        </loadfile>
        <echo>${temp}</echo>
    </target>