Re: Internationalization and Java Strings

Johan Stuyts <[email protected]> Tue, 22 Apr 2008 18:32:04 +0200
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <op.t90tjqrkc4vsc3@audioslave>
> I have been reading up a bit on Internationalization and how the
> Collation API's help out with searching and sorting of Strings etc. But
> if I had a need to store a String to file and this String could have
> weird characters in them how should I be storing them. Should I convert
> it to a byte[] and store them, use the writeUTF() method on one of the
> OutputStream classes or should I be doing something special? The
> requirement is I store them and later retrieve them for various
> comparisons against strings that would have weird characters in them.
> For eg. I would store a string "Canâ" and later retrieve it to compare
> it with some string that gets passed in via some method call. What is
> the best way of doing this? If I store them using writeUTF() and later
> read them using readUTF() would the special characters be stripped out?

If you use properties files by using java.util.Properties, all characters
will be preserved.

Note that if you edit properties files without a proper editor you can
easily create invalid properties files. The properties files must be
encoded using Latin-1 (ISO 8859-1) and all characters that cannot be
represented in Latin-1 must be Unicode escaped. For example, the Euro sign
will become: \u20ac

You can also use Readers and Writers, but in this case you must use an
InputStreamReader and an OutputStreamWriter with an explicit, and fixed
character encoding. If you don't the platform default encoding will be
used and, depending in which environment you run it, everything works
fine, the characters will get garbled, or the file becomes unreadable.

Kind regards,

Johan Stuyts

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com