Java FileWriter, XML and UTF-8

Oddly enough the java.io.FileWriter class doesn’t use UTF-8 by default. I’m not exactly sure what the default encoding is (possibly ISO-8859-1 or US-ASCII?) but it doesn’t seem to be UTF-8, which is odd given that java strings are supposed to be unicode. This causes a problem if you want to have non-ascii characters and you don’t realise what’s happening. This was a bug in SQLEditor and somebody accidentally typed an umlaut into one of the fields and the file wouldn’t reload. (Which was annoying).

The correct thing to do seems to be to use the following:

OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");

Which ensures that you are using UTF-8.

I suppose that the motivation for this is that it means that simple use of FileWriter is compatible with applications that are not unicode aware and don’t support UTF-8. It probably makes sense at some level, but it just goes to show that you can’t assume anything. :-)

4 Responses to “Java FileWriter, XML and UTF-8”

  1. Florian Says:

    That’s exactly the line of code I needed. You are currently no. 2 in a google search for “FileWriter UTF-8″ ;-)

    The java input/ouput api sooo unintuitive - and when someone actually wrote an easy to use FileWriter class he forgot to implement a setEncoding(…).

  2. oh Says:

    Thanks!

  3. Severine Says:

    And no. 1 with “java FileWriter UTF-8″
    Danke schön !

  4. laurent Says:

    Thank You !

    your answer is so accurate for my
    “FileWriter UTF8″ google search !!!

    That’s exactly the line of code I needed too !

Leave a Reply