日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Properties與ResourceBundle的基本使用以及區別

作者:搏·夢 更新時間: 2022-05-25 編程語言

文章目錄

  • 1. 區別
  • 2. 用法
    • 1. Properties解析文件
    • 2. ResourceBundle

1. 區別

  1. Properties 用來解析普通屬性文件
  2. ResourceBundle 通常用于解析國際化資源屬性文件, 會根據本地環境自動選擇對應的國際化資源

2. 用法

1. Properties解析文件

  1. 先準備一個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

  1. 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));
    		        }
    		    }
    		}
    

    在這里插入圖片描述

  2. 但 ResourceBundle 核心的作用是 用于解析國際化資源屬性文件, 會根據本地環境自動選擇對應的國際化資源。

  3. 比如 在中國 name:小明,age:18,但在美國name=xiao_ming,age=eighteen,如下:
    準備兩個properties文件
    test_en_us.properties

    name=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

欄目分類
最近更新