Search code examples
javaencodingantbyte-order-mark

Writing text file in Ant with UTF-16 encoding and BOM


I need to generate a file in my ant project that looks like this:

FF FE 5B 00 6F 00 6C 00|65 00 64 00 62 00 5D 00 |˙ţ[ o l e d b ]
0D 00 0A 00 3B 00 20 00|45 00 76 00 65 00 72 00 |. . ;   E v e r 

The point is:

  • start with BOM
  • line endings 0D 0A
  • UTF-16 encoding

This is what I have at the moment:

<echo  file='${oledir}/cst.udl' append='false' encoding='UTF-16LE'>
[oledb]
;  Ever
</echo>

But the BOM is missing and line endings are not OK because I use Windows.


Solution

  • This appears to work for me, Ant 1.8.0 on Java 1.6.0:

    <concat destfile='cst.udl' append='false' outputencoding='UnicodeLittle'>
    <string>[oledb]
    ; Ever</string>
    <filterchain>
        <fixcrlf eol="dos"/>
    </filterchain>
    </concat>
    

    Uses the Ant concat task with a filterchain to enforce the DOS line endings. The UnicodeLittle encoding includes the BOM in the output, whereas UTF-16LE does not.

    (Note that in the text of your echo task you start the text with a newline:

    <echo ... >
    text
    </echo>
    

    whereas

    <echo ... >text</echo>
    

    contains no newlines.)