Java屬性文件操作之Properties與ResourceBundle詳解
一、Properties與ResourceBundle
兩個類都可以讀取屬性文件中以key/value形式存儲的鍵值對,ResourceBundle讀取屬性文件時操作相對簡單。
二、Properties
該類繼承Hashtable,將鍵值對存儲在集合中?;谳斎肓鲝膶傩晕募凶x取鍵值對,load()方法調(diào)用完畢,就與輸入流脫離關(guān)系,不會自動關(guān)閉輸入流,需要手動關(guān)閉。
public class PropertiesRead {
/**
* 基于輸入流讀取屬性文件:Properties繼承了Hashtable,底層將key/value鍵值對存儲在集合中,
* 通過put方法可以向集合中添加鍵值對或者修改key對應(yīng)的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);// 將文件的全部內(nèi)容讀取到內(nèi)存中,輸入流到達結(jié)尾
fis.close();// 加載完畢,就不再使用輸入流,程序未主動關(guān)閉,需要手動關(guān)閉
/*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中的數(shù)據(jù)寫入xml文件中
props.storeToXML(fos, "來自屬性文件的數(shù)據(jù)庫連接四要素");
System.out.println();
System.out.println("遍歷屬性文件");
System.out.println("hashtable中鍵值對數(shù)目=" + 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 {
//用到的常量又和全局相關(guān)聲明成 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類。使用這個類可以輕松地本地化或翻譯成不同的語言。其中g(shù)etBundle(String baseName)方法的作用是baseName - 資源包的基本名稱,是一個完全限定類名
具有給定基本名稱和默認語言環(huán)境的資源包
*/
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,然后根據(jù).properties文件的名稱信息(本地化信息),匹配當(dāng)前系統(tǒng)的國別語言信息(也可以程序指定),然后獲取相應(yīng)的properties文件的內(nèi)容。
使用這個類,要注意的一點是,這個properties文件的名字是有規(guī)范的:一般的命名規(guī)范是: 自定義名_語言代碼_國別代碼.properties,
如果是默認的,直接寫為:自定義名.properties
比如:
- myres_en_US.properties
- myres_zh_CN.properties
- myres.properties
當(dāng)在中文操作系統(tǒng)下,如果myres_zh_CN.properties、myres.properties兩個文件都存在,則優(yōu)先會使用myres_zh_CN.properties。當(dāng)myres_zh_CN.properties不存在時候,會使用默認的myres.properties。 沒有提供語言和地區(qū)的資源文件是系統(tǒng)默認的資源文件。
資源文件都必須是ISO-8859-1編碼,因此,對于所有非西方語系的處理,都必須先將之轉(zhuǎn)換為Java Unicode Escape格式。轉(zhuǎn)換方法是通過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
運行結(jié)果:
好
好
good
認識Locale Locale 對象表示了特定的地理、政治和文化地區(qū)。需要 Locale 來執(zhí)行其任務(wù)的操作稱為語言環(huán)境敏感的 操作,它使用 Locale 為用戶量身定制信息。
例如,顯示一個數(shù)值就是語言環(huán)境敏感的操作,應(yīng)該根據(jù)用戶的國家、地區(qū)或文化的風(fēng)俗/傳統(tǒng)來格式化該數(shù)值。
使用此類中的構(gòu)造方法來創(chuàng)建 Locale:
- Locale(String language)
- Locale(String language, String country)
- Locale(String language, String country, String variant)
創(chuàng)建完 Locale 后,就可以查詢有關(guān)其自身的信息。使用 getCountry 可獲取 ISO 國家代碼,使用 getLanguage 則獲取 ISO 語言代碼??捎檬褂?getDisplayCountry 來獲取適合向用戶顯示的國家名。同樣,可用使用 getDisplayLanguage 來獲取適合向用戶顯示的語言名。有趣的是,getDisplayXXX 方法本身是語言環(huán)境敏感的,它有兩個版本:一個使用默認的語言環(huán)境作為參數(shù),另一個則使用指定的語言環(huán)境作為參數(shù)。
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));//這個方法設(shè)置的可以跟多個參數(shù).
}
}輸出: 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
到此這篇關(guān)于Java屬性文件操作之Properties與ResourceBundle詳解的文章就介紹到這了,更多相關(guān)Java的Properties與ResourceBundle內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis?超詳細講解動態(tài)SQL的實現(xiàn)
動態(tài)?SQL?是?MyBatis?的強大特性之一。如果你使用過?JDBC?或其它類似的框架,你應(yīng)該能理解根據(jù)不同條件拼接?SQL?語句有多痛苦,例如拼接時要確保不能忘記添加必要的空格,還要注意去掉列表最后一個列名的逗號。利用動態(tài)?SQL,可以徹底擺脫這種痛苦2022-03-03
spring如何實現(xiàn)依賴注入DI(spring-test方式)
本文主要介紹如何實現(xiàn)spring 的依賴注入,并且淺顯的講述一下注入需要注意的事項。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
java GUI實現(xiàn)學(xué)生圖書管理簡單實例
這篇文章主要為大家詳細介紹了java GUI實現(xiàn)學(xué)生圖書管理簡單示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
java工具類SendEmailUtil實現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細介紹了java工具類SendEmailUtil實現(xiàn)發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02

