一、Properties与ResourceBundle
两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单。
二、Properties
该类继承Hashtable,将键值对存储在集合中。基于输入流从属性文件中读取键值对,load()方法调用完毕,就与输入流脱离关系,不会自动关闭输入流,需要手动关闭。
public class PropertiesRead {
/**
* 基于输入流读取属性文件:Properties继承了Hashtable,底层将key/value键值对存储在集合中,
* 通过put方法可以向集合中添加键值对或者修改key对应的value
*
* @throws IOException
*/
@SuppressWarnings("rawtypes")
@Test
public void test01() throws IOException {
FileInputStream fis = new FileInputStream("src/database.properties");
Properties props = new Properties();
props.load(fis);// 将文件的全部内容读取到内存中,输入流到达结尾
fis.close();// 加载完毕,就不再使用输入流,程序未主动关闭,需要手动关闭
/*byte[] buf = new byte[1024];
int length = fis.read(buf);
System.out.println("content=" + new String(buf, 0, length));//抛出StringIndexOutOfBoundsException*/
System.out.println("driver=" + props.getProperty("driver"));
System.out.println("url=" + props.getProperty("url"));
System.out.println("username=" + props.getProperty("username"));
System.out.println("password=" + props.getProperty("password"));
System.out.println("database_name=" + props.getProperty("database_name"));
/**
* Properties其他可能用到的方法
*/
props.put("serverTimezone", "UTC");// 底层通过hashtable.put(key,value)
props.put("jdbc.password", "456");
FileOutputStream fos = new FileOutputStream("src/database.xml");// 将Hashtable中的数据写入xml文件中
props.storeToXML(fos, "来自属性文件的数据库连接四要素");
System.out.println();
System.out.println("遍历属性文件");
System.out.println("hashtable中键值对数目=" + props.size());
Enumeration keys = props.propertyNames();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
System.out.println(key + "=" + props.getProperty(key));
}
}
}
三、ResourceBundle
国际化。
- 示例代码
import java.util.ResourceBundle;
public class ResourceBundleAction {
//用到的常量又和全局相关声明成 static 和 final 类型的.
public static final String CLASSDRIVER ;
public static final String URL ;
public static final String NAME ;
public static final String PASSWORD ;
static{
/*
javase中的提供的一个国际化的类 ResourceBundle类。使用这个类可以轻松地本地化或翻译成不同的语言。其中getBundle(String baseName)方法的作用是baseName - 资源包的基本名称,是一个完全限定类名
具有给定基本名称和默认语言环境的资源包
*/
CLASSDRIVER = ResourceBundle.getBundle("db").getString("driverClass");
URL = ResourceBundle.getBundle("db").getString("url");
NAME = ResourceBundle.getBundle("db").getString("name");
PASSWORD = ResourceBundle.getBundle("db").getString("password");
}
}
- java.util.ResourceBundle使用详解
说的简单点,这个类的作用就是读取资源属性文件properties,然后根据.properties文件的名称信息(本地化信息),匹配当前系统的国别语言信息(也可以程序指定),然后获取相应的properties文件的内容。
使用这个类,要注意的一点是,这个properties文件的名字是有规范的:一般的命名规范是: 自定义名_语言代码_国别代码.properties,
如果是默认的,直接写为:自定义名.properties
比如:
myres_en_US.properties
myres_zh_CN.properties
myres.properties
当在中文操作系统下,如果myres_zh_CN.properties、myres.properties两个文件都存在,则优先会使用myres_zh_CN.properties。当myres_zh_CN.properties不存在时候,会使用默认的myres.properties。
没有提供语言和地区的资源文件是系统默认的资源文件。
资源文件都必须是ISO-8859-1编码,因此,对于所有非西方语系的处理,都必须先将之转换为Java Unicode Escape格式。转换方法是通过JDK自带的工具native2ascii.
定义三个资源文件,放到src的根目录下面(必须这样,或者你放到自己配置的calsspath下面)。
TestResourceBundle.java
public class TestResourceBundle {
public static void main(String[] args) {
Locale locale1 = new Locale("zh", "CN");
ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);
System.out.println(resb1.getString("aaa"));
ResourceBundle resb2 = ResourceBundle.getBundle("myres", Locale.getDefault());
System.out.println(resb1.getString("aaa"));
Locale locale3 = new Locale("en", "US");
ResourceBundle resb3 = ResourceBundle.getBundle("myres", locale3);
System.out.println(resb3.getString("aaa"));
}
}
在src根目录下有:
myres.properties:
aaa=good
bbb=thanks
myres_en_US.properties:
aaa=good
bbb=thanks
myres_zh_CN.properties
aaa=\u597d
bbb=\u591a\u8c22
运行结果:
好
好
good
- 认识Locale
Locale 对象表示了特定的地理、政治和文化地区。需要 Locale 来执行其任务的操作称为语言环境敏感的 操作,它使用 Locale 为用户量身定制信息。例如,显示一个数值就是语言环境敏感的操作,应该根据用户的国家、地区或文化的风俗/传统来格式化该数值。
使用此类中的构造方法来创建 Locale:
Locale(String language)
Locale(String language, String country)
Locale(String language, String country, String variant)
创建完 Locale 后,就可以查询有关其自身的信息。使用 getCountry 可获取 ISO 国家代码,使用 getLanguage 则获取 ISO 语言代码。可用使用 getDisplayCountry 来获取适合向用户显示的国家名。同样,可用使用 getDisplayLanguage 来获取适合向用户显示的语言名。有趣的是,getDisplayXXX 方法本身是语言环境敏感的,它有两个版本:一个使用默认的语言环境作为参数,另一个则使用指定的语言环境作为参数。
public class I18nMessages {
public static final String KEY_NOT_FOUND_PREFIX = "!!!"; //$NON-NLS-1$
public static final String KEY_NOT_FOUND_SUFFIX = "!!!"; //$NON-NLS-1$
private static final String BUNDLE_NAME = "messages"; //$NON-NLS-1$
private static final String BUNDLE_NAME2 = "cn.itcast.interfaceAbstract.messages"; //$NON-NLS-1$
private static final String PLUGIN_ID = "cn.itcast.resourcebundle"; //$NON-NLS-1$
private static ResourceBundle resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME2);
public static String getString(String key, String pluginId, ResourceBundle resourceBundle) {
if (resourceBundle == null) {
return KEY_NOT_FOUND_PREFIX + key + KEY_NOT_FOUND_SUFFIX;
}
try {
return resourceBundle.getString(key);
} catch (MissingResourceException e) {
return KEY_NOT_FOUND_PREFIX + key + KEY_NOT_FOUND_SUFFIX;
}
}
public static String getString(String key, String pluginId, ResourceBundle resourceBundle, Object... args) {
return MessageFormat.format(getString(key, pluginId, resourceBundle), args);
}
public static String getString(String key, Object... args) {
return getString(key, PLUGIN_ID, resourceBundle, args);
}
public static void main(String[] args) {
String test = "kxh";
String test2 = "Yes,I am";
System.out.println(I18nMessages.getString("name", test, test2));//这个方法设置的可以跟多个参数.
}
}
输出:
ResourceBundle.getBundle(BUNDLE_NAME2);的时候
Are you kxh?
Yes,I am
This is the second one
ResourceBundle.getBundle(BUNDLE_NAME);的时候
Are you kxh?
Yes,I am
This is the first one