Java Reference
In-Depth Information
Example 7−5: LocalizedError.java (continued)
public static void main(String[] args) {
try { FileReader in = new FileReader(args[0]); }
catch(Exception e) { LocalizedError.display(e); }
}
}
The following listing shows the resource bundle properties file used to localize the
set of possible error messages that can be thrown by the ConvertEncoding class
shown at the beginning of this chapter:
#
# This is the file Errors.properties
# One property for each class of exceptions that our program might
# report. Note the use of backslashes to continue long lines onto the
# next. Also note the use of \n and \t for newlines and tabs
#
java.io.FileNotFoundException: \
Error: File "{0}" not found\n\t\
Error occurred at line {3} of file "{2}"\n\tat {4}
java.io.UnsupportedEncodingException: \
Error: Specified encoding not supported\n\t\
Error occurred at line {3} of file "{2}"\n\tat {4,time} on {4,date}
java.io.CharConversionException:\
Error: Character conversion failure. Input data is not in specified format.
# A generic resource. Display a message for any error or exception that
# is not handled by a more specific resource.
java.lang.Throwable:\
Error: {1}: {0}\n\t\
Error occurred at line {3} of file "{2}"\n\t{4,time,long} {4,date,long}
With a resource bundle like this, ConvertEncoding produces error messages like
the following:
Error: File "myfile (No such file or directory)" not found
Error occurred at line 64 of file "FileInputStream.java"
at 7/9/00 9:28 PM
Or, if the current locale is fr_FR :
Error: File "myfile (Aucun fichier ou répertoire de ce type)" not found
Error occurred at line 64 of file "FileInputStream.java"
at 09/07/00 21:28
Exercises
7-1. Several internationalization-related classes, such as NumberFormat and Date-
Format , have static methods named getAvailableLocales() that returnan
array of the Locale objects they support. You can look up the name of the
country of a given Locale object with the getDisplayCountry() method. Note
that this method has two variants. One takes no arguments and displays the
country name as appropriate in the default locale. The other version of
Search WWH ::




Custom Search