2

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

lospejos
  • 1,976
  • 3
  • 19
  • 35
Robert Strauch
  • 12,055
  • 24
  • 120
  • 192

2 Answers2

4

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.

buruzaemon
  • 3,847
  • 1
  • 23
  • 44
0

Remember, Groovy is based on Java. Just write:

File outFile = new File(filePath)
Writer out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(outFile), "UTF8"));
out.append(strBuf.toString())
out.flush()
out.close()