Translating Between Unicode and Non-Unicode Character Sets in Java | 2 | WebReference

Translating Between Unicode and Non-Unicode Character Sets in Java | 2


Translating Between Unicode and Non-Unicode Character Sets in Java [con't]

Converting Character and Byte Streams

The package provides classes that allow you to convert between Unicode character streams and byte streams of non-Unicode text. You can use the InputStreamReader class to convert byte streams to character streams. When you create an InputStreamReader object, you specify the byte encoding that you want to convert. For example, to translate a text file in the UTF-8 encoding into Unicode, you would create an InputStreamReader as follows:

The OutputStreamWriter class operates in much the same fashion and can be used to output text in character sets other than Unicode. The following code creates and writes UTF-8 characters to a file. Again, the code is enclosed in a try block, in case the specified encoding is not supported:

If you omit the encoding identifier, InputStreamReader and OutputStreamWriter use the default encoding. You can determine which encoding an InputStreamReader or OutputStreamWriter uses by calling the getEncoding() method, as follows:

You can also use an InputStreamReader object to convert the bytes from UTF-8 into Unicode and save the result to a String. In the following example, the InputStreamReader class acts as a bridge from byte streams to character streams. It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted:

Having covered character translation, I have now touched upon every facet of an existing localized application that may have to be modified in order to convert it into a global one. My next several articles will expound upon each area contained in the Internationalizing a Locale-specific Application in Java article, starting with localization.

Have a suggestion for an article topic? Do you have a product or service that you'd like reviewed? Email it to Rob .

Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. Rob's latest, entitled KNIGHTFALL, was a collaboration between himself, the former Ivory Knight vocalist, and legendary guitarist/producer, Jeff Waters of Annihilator fame. Rob is available for software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!

Original: April 26, 2010