Re: Internationalization and Java Strings

Endre Stølsvik <[email protected]> Tue, 22 Apr 2008 18:36:13 +0200
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <[email protected]>
Avinash Lakshman wrote:
> Hi
>
> 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?

I don't believe this qualifies as "advanced java" in any way.

When you "convert to a byte[]", that's when the magic happens: this is
the _encoding_ phase. Similarly, when you convert from bytes back to
Character arrays (Strings), you've got the _decoding_ phase.

Here's for output:
   http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html

Here's for input:
   http://java.sun.com/javase/6/docs/api/java/io/InputStreamReader.html

Here's the java.nio.charset package, another option of converting. I
believe the above referenced classes these days actually use this stuff:

http://java.sun.com/javase/6/docs/api/java/nio/charset/package-summary.html

UTF-8 is the best charset for nearly everything, is pretty much the
internet default, and handles almost EVERY "weird" character. the
[read|write]UTF methods use a special "Modified UTF-8" format, not quite
what you want.

Kind regards,
Endre.

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

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