[Common Java class libraries] _ International Programs
Instance 1: (the message does not need to be added to properties, and the program will automatically identify the message)
Import Java. util. resourcebundle; public class interdemo01 {public static void main (string ARGs []) {resourcebundle RB = resourcebundle. getbundle ("message"); // find the resource file without the suffix system. out. println ("content:" + RB. getstring ("info"); // get content from the resource file }};
Message. Properties
Info = 1111 hello
Example 2: (use locale for Region recognition and format with messageformat)
Import Java. util. resourcebundle; import Java. util. locale; import Java. text. *; public class interdemo03 {public static void main (string ARGs []) {locale zhloc = new locale ("ZH", "cn "); // indicates locale enloc = new locale ("en", "us") in China; // indicates locale frloc = new locale ("FR", "FR") in the United States "); // indicates the French region // find the Chinese attribute file. You must specify the Chinese locale object resourcebundle zhrb = resourcebundle. getbundle ("message", zhloc); // locate the attribute file in English. You must specify the locale object resourcebundle enrb = resourcebundle. getbundle ("message", enloc); // find the French attribute file. You must specify the French locale object resourcebundle FRRB = resourcebundle. getbundle ("message", frloc); // read the content of each attribute file in sequence and read it by key value. At this time, the key value name is unified to Info string str1 = zhrb. getstring ("info"); string str2 = enrb. getstring ("info"); string str3 = FRRB. getstring ("info"); system. out. println ("Chinese:" + messageformat. format (str1, "Li Xinghua"); system. out. println ("English:" + messageformat. format (str2, "lixinghua"); system. out. println ("French:" + messageformat. format (str3, "lixinghua "));}};
Message_zh_cn.properties (the content is obtained using \ jdk1.6.0 _ 10 \ bin \ native2ascii.exe)
Info = \ u4f60 \ u597d \ uff0c {0} \ uff01
Message_en_us.properties
Info = Hello, {0 }!
Message_fr_fr.properties
Info = bonjour, {0 }!
Example 3: (Variable Parameter 1)
Public class interdemo04 {public static void main (string ARGs []) {system. out. print ("first run:"); fun ("lxh", "Li", "Li Xinghua"); // input three parameters: system. out. print ("\ n second run:"); fun ("mldn"); // input a parameter} public static void fun (object... ARGs) {// fixed syntax. Input any number of data and use an array to represent for (INT I = 0; I <args. length; I ++) {system. out. print (ARGs [I] + ",");}}};
Example 4: (Variable Parameter 2)
Public class interdemo05 {public static void main (string ARGs []) {system. out. print ("first run:"); object [] arg1 = {"lxh", "Li", "Li Xinghua"}; fun (arg1 ); // input three parameters: system. out. print ("\ n second run:"); object [] arg2 = {"mldn"}; fun (arg2); // input a parameter system. out. print ("\ n third run:"); object [] arg3 ={}; // No parameter is input for fun (arg3);} public static void fun (object... ARGs) {// fixed syntax. Input any number of data and use an array to represent for (INT I = 0; I <args. length; I ++) {system. out. print (ARGs [I] + ",");}}};
Instance 5: (resource files are not used and called using the resource class)
Resource Type:
Import java. util. listresourcebundle; public class message_zh_cn extends listresourcebundle {private final object data [] [] = {"info", "Chinese, hello, {0 }! "}}; Public object [] [] getcontents () {// override method return data ;}};
Caller:
Import Java. util. resourcebundle; import Java. util. locale; import Java. text. *; public class interdemo06 {public static void main (string ARGs []) {locale zhloc = new locale ("ZH", "cn "); // indicates the Chinese region // find the Chinese attribute file. You must specify the Chinese locale object resourcebundle zhrb = resourcebundle. getbundle ("message", zhloc); string str1 = zhrb. getstring ("info"); system. out. println ("Chinese:" + messageformat. format (str1, "Li Xinghua "));}};