聊聊ResourceBundle和properties讀取配置文件的區(qū)別
java.util.ResourceBundle 和java.util.properties 讀取配置文件區(qū)別
這兩個類都是讀取properties格式的文件的,而Properties同時還能用來寫文件。
Properties的處理方式是將其作為一個映射表,而且這個類表示了一個持久的屬性集,他是繼承HashTable這個類。
ResourceBundle本質(zhì)上也是一個映射,但是它提供了國際化的功能。
假設(shè)電腦設(shè)置的地區(qū)是中國大陸,語言是中文
那么你向ResourceBundle(資源約束名稱為base)獲取abc變量的值的時候,ResourceBundle會先后搜索
base_zh_CN_abc.properties
base_zh_CN.properties
base_zh.properties
base.properties
文件,直到找到abc為止
相應(yīng)的,在英國就會去找base_en_GB_abc.properties等。
因此,你只需要提供不同語言的資源文件,而無需改變代碼,就達(dá)到了國際化的目的。
另外,在.properties里面,不能直接使用中文之類文字,而是要通過native2ascii轉(zhuǎn)乘\uxxxx這種形式
附:
1.編碼問題:
無論系統(tǒng)的默認(rèn)編碼是什么,ResourceBundle在讀取properties文件時統(tǒng)一使用iso8859-1編碼。
因此,如果在默認(rèn)編碼為 GBK的系統(tǒng)中編寫了包含中文的properties文件,經(jīng)由ResourceBundle讀入時,必須轉(zhuǎn)換為GBK格式的編碼,否則不能正確識別。
2.用法:
ResourceBundle:
ResourceBundle conf= ResourceBundle.getBundle("config/fnconfig/fnlogin"); String value= conf.getString("key");
Properties:
Properties prop = new Properties(); try { InputStream is = getClass().getResourceAsStream("xmlPath.properties"); prop.load(is); //或者直接prop.load(new FileInputStream("c:/xmlPath.properties")); if (is != null) { is.close(); } } catch (Exception e) { System.out.println( "file " + "catalogPath.properties" + " not found!\n" + e); } String value= prop.getProperty("key").toString();
ResourceBundle 讀取Properties文件及亂碼處理
package read; import java.util.ResourceBundle; /** * 屬性文件工廠類 * @author W * @version V1.0 * @date 2013-4-17 */ public interface ReadPropertiesFactory { public ResourceBundle getErrorResource(); } ================================================ package read; import java.util.ResourceBundle; /** * * @author * @version V1.0 * @date 2013-5-13 */ public class ReadPropertiesFactoryImpl implements ReadPropertiesFactory { private ResourceBundle errorResouce; public ResourceBundle getErrorResource() { if(errorResouce == null){ //只要讀取properties的名稱就可以了 errorResouce = ResourceBundle.getBundle("errorMessage"); } return errorResouce; } } =============================================== package util; import java.io.UnsupportedEncodingException; /** * * @author * @version V1.0 * @date 2013-4-17 */ public class StringHanlder { public static String transformCodeIso8859Style(String code , String codeStyle) throws UnsupportedEncodingException{ return new String(code.getBytes("ISO-8859-1"),codeStyle); } public static String transformCodeUtf8Style(String code , String codeStyle) throws UnsupportedEncodingException{ return new String(code.getBytes("utf-8"),codeStyle); } } ========================================================================= errorMessage.properties文件中的屬性 E01010024=查詢數(shù)據(jù)異常! ============================================================================= package www.man.comService; import java.util.ResourceBundle; import read.ReadPropertiesFactoryImpl; public class TestService {public static void main(String[] args) { String a= TestService.getErrorValue("E01010070");System.out.println(a);} private static ResourceBundle getErrorResource() { ReadPropertiesFactoryImpl readPropertiesFactory =new ReadPropertiesFactoryImpl(); return readPropertiesFactory.getErrorResource(); } public static String getErrorValue(String key){ try{ return util.StringHanlder.transformCodeIso8859Style(getErrorResource().getString(key),"utf-8"); }catch(Exception e){ e.printStackTrace();return ""; } }}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA Stack詳細(xì)介紹和示例學(xué)習(xí)
JAVA Stack是棧。它的特性是:先進后出(FILO, First In Last Out)。2013-11-11springboot的SpringPropertyAction事務(wù)屬性源碼解讀
這篇文章主要介紹了springboot的SpringPropertyAction事務(wù)屬性源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11Java超詳細(xì)教你寫一個學(xué)籍管理系統(tǒng)案例
這篇文章主要介紹了怎么用Java來寫一個學(xué)籍管理系統(tǒng),學(xué)籍管理主要涉及到學(xué)生信息的增刪查改,本篇將詳細(xì)的實現(xiàn),感興趣的朋友跟隨文章往下看看吧2022-03-03Java中ArrayList與順序表的定義與實現(xiàn)方法
ArrayList是一個實現(xiàn)List接口的類,底層是動態(tài)類型順序表,本質(zhì)也就是數(shù)組,動態(tài)主要體現(xiàn)在它的擴容機制,下面這篇文章主要給大家介紹了關(guān)于Java中ArrayList與順序表的定義與實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-07-07Java基于IDEA實現(xiàn)qq郵件發(fā)送小程序
這篇文章主要介紹了Java基于IDEA實現(xiàn)qq郵件發(fā)送小程序功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09SpringBoot整合RabbitMQ實現(xiàn)消息確認(rèn)機制
這篇文章主要介紹了SpringBoot整合RabbitMQ實現(xiàn)消息確認(rèn)機制,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08