Search code examples
javaencodingutf-8character-encodingiso-8859-1

encode œ correctly with UTF-8 - java


I am having problems to write out the following string into a file correctly. Especially with the character "œ". The Problem appears on my local machine (Windows 7) and on the server (Linux)

String: "Cœurs d’artichauts grillées"

  1. Does Work (œ gets displays correctly, while the apostrophe get translated into a question mark):

    Files.write(path, content.getBytes(StandardCharsets.ISO_8859_1));
    
  2. Does not work (result in file):

    Files.write(path, content.getBytes(StandardCharsets.UTF_8));
    

According to the first answer of this question, UTF-8 should be able to encode the œ correctly as well. Has anyone have an idea what i am doing wrong?


Solution

  • Your second approach works

    String content = "Cœurs d’artichauts grillées";
    Path path = Paths.get("out.txt");
    Files.write(path, content.getBytes(Charset.forName("UTF-8")));
    

    Is producing an out.txt file with:

    Cœurs d’artichauts grillées
    

    Most likely the editor you are using is not displaying the content correctly. You might have to force your editor to use the UTF-8 encoding and a font that displays œ and other UTF-8 characters. Notepad++ or IntelliJ IDEA work out of the box.