Search code examples
groovyutf-8file-writing

Groovy script does not write file as UTF-8


I have the following piece of code:

def f = new File("test.txt")
f.write("test", "UTF-8")

When opening the file in Notepad++ (or any other editor) it is still in ISO-8859-1 instead of UTF-8. Opening the file in a hex editor it does not contain the "magic bytes" 0xEFBBBF.

Regards,
Robert


Solution

  • UTF-8 files do not really require the Byte-Order Mark indicator.

    For example, if your UTF-8 file only contains ASCII chars, the file utility will return this:

    $ file [filename]
    ASCII text
    

    But when you introduce, say, Japanese chars into that file, then file will return this:

    UTF-8 Unicode text
    

    .. but the file will not begin with the BOM.