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)存中,輸入流到達(dá)結(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 - 資源包的基本名稱,是一個完全限定類名
具有給定基本名稱和默認(rèn)語言環(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,
如果是默認(rèn)的,直接寫為:自定義名.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不存在時候,會使用默認(rèn)的myres.properties。 沒有提供語言和地區(qū)的資源文件是系統(tǒng)默認(rèn)的資源文件。
資源文件都必須是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
認(rèn)識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)境敏感的,它有兩個版本:一個使用默認(rè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)文章
SpringBoot整合FFmpeg進(jìn)行視頻處理的詳細(xì)教學(xué)
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合FFmpeg進(jìn)行視頻處理,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2026-01-01
Springboot整合JwtHelper實現(xiàn)非對稱加密
本文主要介紹了Springboot整合JwtHelper實現(xiàn)非對稱加密,主要介紹兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Spring boot框架下的RabbitMQ消息中間件詳解
這篇文章詳細(xì)介紹了Spring Boot框架下的RabbitMQ消息中間件的基本概念、消息傳輸模型、環(huán)境準(zhǔn)備、Spring Boot集成以及消息生產(chǎn)和消費,感興趣的朋友跟隨小編一起看看吧2025-01-01
SpringBoot接入deepseek深度求索示例代碼(jdk1.8)
這篇文章主要介紹了SpringBoot接入deepseek深度求索的相關(guān)資料,包括建API?key、封裝詢問Deepseek的工具方法(在配置文件中添加key值)、調(diào)用測試并確保端口一致例如8091,最后運行結(jié)果,需要的朋友可以參考下2025-02-02
Java統(tǒng)計字符串中字符出現(xiàn)次數(shù)的方法示例
這篇文章主要介紹了Java統(tǒng)計字符串中字符出現(xiàn)次數(shù)的方法,涉及Java針對字符串的遍歷、查找、計算等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
SpringAMQP消息隊列(SpringBoot集成RabbitMQ方式)
這篇文章主要介紹了SpringAMQP消息隊列(SpringBoot集成RabbitMQ方式),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

