-1

I want to store a UTF-8 string "Русслэнд" in a Java properties file as UTF-8 literals like this "\u0420\u0443\u0441\u0441\u043b\u044d\u043d\u0434"

How can I convert this? Since Java properties files are ISO-8859-1 by default.

Shlomo
  • 3,880
  • 8
  • 50
  • 82
  • 1
    Does this help? http://stackoverflow.com/questions/4659929/how-to-use-utf-8-in-resource-properties-with-resourcebundle – Diego Martinoia Apr 08 '15 at 09:44
  • Thanks, is there a way without ResourceBundle? – Shlomo Apr 08 '15 at 09:48
  • You can also do Properties.load(reader) and pass a UTF8 compatible reader. – Diego Martinoia Apr 08 '15 at 09:50
  • 2
    Take care to use the correct terminology. Those are not "[UTF-8](http://en.wikipedia.org/wiki/UTF-8) literals", but UTF-16 character literals. You're putting people on the wrong track by mentioning UTF-8. – Jesper Apr 08 '15 at 09:51
  • Also, there's no such thing as an "UTF-8 string". Strings are just sequences of characters. When you convert them to bytes, then you use a character encoding. But a string doesn't have a character encoding by itself. (Java internally uses UTF-16 to store characters). – Jesper Apr 08 '15 at 09:53

2 Answers2

1

Try StringEscapeUtils.unescapeJava()

StringEscapeUtils

RSCh
  • 171
  • 6
1

Java has a command line tool to do conversions

native2ascii -encoding UTF-8 x-utf8.txt x.properties
native2ascii -reverse -encoding UTF-8 x.properties x2-utf8.txt

Also there are comfortable properties editors that let you enter in unescaped Unicode, and can display several languages side-by-side. For instance in the standard NetBeans IDE.

Also possible is to keep the UTF-8 file as source and have maven or ant copy and convert the file to the build directory.

And then there is the option to load Properties with an encoding; a bit too specific.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138