Java Reference
In-Depth Information
Example 7−2: ConvertEncoding.java (continued)
// Set up character streams.
Reader r = new BufferedReader(new InputStreamReader(in, from));
Writer w = new BufferedWriter(new OutputStreamWriter(out, to));
// Copy characters from input to output. The InputStreamReader
// converts from the input encoding to Unicode, and the
// OutputStreamWriter converts from Unicode to the output encoding.
// Characters that cannot be represented in the output encoding are
// output as '?'
char[] buffer = new char[4096];
int len;
while((len = r.read(buffer)) != -1) // Read a block of input.
w.write(buffer, 0, len);
// And write it out.
r.close();
// Close the input.
w.close();
// Flush and close output.
}
}
Handling Local Customs
The second problem of internationalization is the task of following local customs
and conventions in areas such as date and time formatting. The java.text pack-
age defines classes to help with this duty.
The NumberFormat class formats numbers, monetary amounts, and percentages in a
locale-dependent way for display to the user. This is necessary because different
locales have different conventions for number formatting. For example, in France,
a comma is used as a decimal separator instead of a period, as in many English-
speaking countries. A NumberFormat object can use the default locale or any locale
you specify.
The DateFormat class formats dates and times in a locale-dependent way for dis-
play to the user. Different countries have different conventions. Should the month
or day be displayed first? Should periods or colons separate fields of the time?
What are the names of the months in the language of the locale? A DateFormat
object can simply use the default locale, or it can use any locale you specify. The
DateFormat class is used in conjunction with the TimeZone and Calendar classes of
java.util . The TimeZone object tells the DateFormat what time zone the date
should be interpreted in, while the Calendar object specifies how the date itself
should be broken down into days, weeks, months, and years. Almost all locales
use the standard GregorianCalendar .
The Collator class compares strings in a locale-dependent way. This is necessary
because different languages alphabetize strings in different ways (and some lan-
guages don't even use alphabets). In traditional Spanish, for example, the letters
“ch” are treated as a single character that comes between “c” and “d” for the pur-
poses of sorting. When you need to sort strings or search for a string within Uni-
code text, you should use a Collator object, either one created to work with the
default locale or one created for a specified locale.
The BreakIterator class allows you to locate character, word, line, and sentence
boundaries in a locale-dependent way. This is useful when you need to recognize
Search WWH ::




Custom Search