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