網站首頁 編程語言 正文
文章目錄
- 1. 區別
- 2. 用法
- 1. Properties解析文件
- 2. ResourceBundle
1. 區別
- Properties 用來解析普通屬性文件
- ResourceBundle 通常用于解析國際化資源屬性文件, 會根據本地環境自動選擇對應的國際化資源
2. 用法
1. Properties解析文件
-
先準備一個properties文件放在resource目錄下:
name=小明 age=18
public class demo2 { public static void main(String[] args) { // ClassPathResource 是用于讀取resource目錄下文件,具體可以看下面的博客 // https://blog.csdn.net/xueyijin/article/details/121441738 try (InputStreamReader in = new InputStreamReader(new ClassPathResource("test.properties").getInputStream())) { Properties p = new Properties(); p.load(in); // Properties的遍歷:https://blog.csdn.net/xueyijin/article/details/123968385 Set<Object> keySet = p.keySet(); for (Object key : keySet) { System.out.println(key + ":" + p.get(key)); } } catch (IOException e) { e.printStackTrace(); } } }
2. ResourceBundle
-
ResourceBundle 本身就是可以解析properties文件,因此也可以使用于解析properties文件
public class demo { public static void main(String[] args) { // 因為ResourceBundle 就是專門用于解析properties文件,因此不用加 .properties 后綴 ResourceBundle resourceBundle = ResourceBundle.getBundle("test"); Set<String> keys = resourceBundle.keySet(); for (String key : keys) { System.out.println(key +":" // 其實ResourceBundle解析文件,有點很麻煩的地方就是這個字符轉化,比較繁瑣 + new String(resourceBundle.getString(key).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8)); } } }
-
但 ResourceBundle 核心的作用是 用于解析國際化資源屬性文件, 會根據本地環境自動選擇對應的國際化資源。
-
比如 在中國 name:小明,age:18,但在美國name=xiao_ming,age=eighteen,如下:
準備兩個properties文件
test_en_us.propertiesname=xiao_ming age=eighteen
test_zh_cn.properties
name=小明 age=18
public class demo3 { public static void main(String[] args) { // ResourceBundle.getBundle("test", new Locale("en","us")) // 這里的意思:先在resource目錄下找 test_en_us.properties文件 // 如果沒有再找 test_en.properties文件 // 如果還是沒有,則找test.properties文件,若還是沒有,則報錯了 // 想 new Locale的兩個參數,就可以改成配置變量,即可切換對應的properties內容了。 ResourceBundle resourceBundle = ResourceBundle.getBundle("test", new Locale("en","us")); final Set<String> keys = resourceBundle.keySet(); for (String key : keys) { System.out.println(key +":" + new String(resourceBundle.getString(key).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8)); } } }
原文鏈接:https://blog.csdn.net/xueyijin/article/details/123975465
相關推薦
- 2022-08-28 keil5仿真相關配置,解決相關bug
- 2022-05-11 并發編程之CAS和Atomic原子操作類
- 2022-05-06 react-router-domV6版本的路由和嵌套路由寫法詳解_React
- 2022-09-04 使用Python去除小數點后面多余的0問題_python
- 2022-06-13 matplotlib圖形整合之多個子圖繪制的實例代碼_python
- 2022-08-05 Redis實現短信驗證碼登錄的示例代碼_Redis
- 2022-05-14 InnoDB主鍵索引樹和二級索引樹的場景分析_數據庫其它
- 2022-11-05 Nginx反向代理location和proxy_pass配置規則詳細總結_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支