2

When I process a properties file with the Spanish characters ó and é, characters are displayed as ?. I tried different ways to fix this, but still fail:

  • I tried to use \uxxxx
  • I tried to use InputStreamReader with encoding UTF-8
  • I tried to convert string to bytes and then create a new String from those bytes:

    new String( val.getBytes("UTF-8"), "UTF-8")
    

Nothing worked. What should I do next to fix this issue? Japanese and Russian are still OK.

Jacob
  • 77,566
  • 24
  • 149
  • 228
Barcelona
  • 2,122
  • 4
  • 21
  • 42
  • 1
    Are Japanese and Russian characters saved in the same properties file? How exactly are you specifying them in there and how exactly are you reading and showing them? – BalusC Nov 23 '11 at 16:13
  • No, each language has its own properties file. I mean messaages_ja_JP.properties, messages_ru_RU.properties and Spanish messages_es_ES.properties – Barcelona Nov 23 '11 at 16:15
  • 1
    You should work out whether the problem is in loading or displaying the string. Then provide a short but complete program demonstrating that part. – Jon Skeet Nov 23 '11 at 16:17
  • Well, that sounds like as the file itself is not saved with the proper encoding, but then the `\uXXXX` should have worked. Can you please answer the two other questions as well? And this new question: how about when you put a Japanese string in the Spanish bundle and the other way round? – BalusC Nov 23 '11 at 16:17
  • Good idea. I use a FileInputStream to read the file and then call Properties.load(new InputStreamreader(is, "UTF-8")) to load the file base on the Locale. WHen I copy Japanese characters to Spanish bundl, Japanese chars converted to '??????' in that file. When I copy Spanish characters to Japanese bundle I fail with both Japanese and Spanish. – Barcelona Nov 23 '11 at 16:32

1 Answers1

4

The properties file needs to be in the proper encoding. By default some IDE's like eclipse saves the content using CP1252 but you are requiring the file as UTF-8. This is also required for your java code.

If you try to use \uxxxx characters but your application by default is working with CP1252 the conversion of the escape code result in a bad character.

If you use the InputStreamReader to force the reading as UTF-8 but your code and/or your file are not using UTF-8 support result in a bad character.

If you use UTF-8 conversion of an string but your source code is CP1252 you should have the same problem.

Related previous answer about source code : Should source code be saved in UTF-8 format

  • Notepad ++ Has a menu to view the format of the file and change it in "Format" menu you should view the file as if it should be opened by other formarts or you should convert the file to other file formats like "UTF-8"
Community
  • 1
  • 1
Dubas
  • 2,855
  • 1
  • 25
  • 37
  • When I open Properties file with Notepad++ it displays OK but when I open it with Netbeans it displays all fail (Japanese and Russian fail too). Why this issue happen??? – Barcelona Nov 23 '11 at 16:39
  • Notepad++ opens the file in the correct format. But Ides like netbeans only in the format specified by the project. – Dubas Nov 23 '11 at 16:41
  • Done, I tried to create new files in NetBeans/Eclipse and it displayed well. Thanks all for your help :) :) :) – Barcelona Nov 23 '11 at 23:39
  • When I tried to edit again that properties file by using Notepad++, my tests fail again. Which editor should I use in Windows and Linux to edit properties files – Barcelona Nov 24 '11 at 00:08
  • Just wanted to add: Beware of Notepad++'s "UTF-8 with BOM" adds the BOM character which might break some things, so use "UTF-8 without BOM"... it's a ticking time-BOM – JKirchartz Jan 30 '12 at 15:29