<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] # Properties Properties 類表示了一個持久的屬性集。Properties 可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。 ~~~ public class Properties extends Hashtable<Object,Object> ~~~ 在使用Properties類的時候不需要設置泛型,因為從它一開始出現就只能保持字符串 特點: 1. Hashtable的子類,map集合中的方法都可以用。 2. 該集合沒有泛型。鍵值都是字符串。 3. 它是一個可以持久化的屬性集。鍵值可以存儲到集合中,也可以存儲到持久化的設備(硬盤、U盤、光盤)上。鍵值的來源也可以是持久化的設備。 4. 有和流技術相結合的方法。 ![](https://box.kancloud.cn/c2083c2c677362f433d72f9a8c8a641c_914x366.png) ~~~ 設置屬性 public synchronized Object setProperty(String key, String value) 取得屬性 public String getProperty(String key) 取得屬性,如果key不存在返回默認值 public String getProperty(String key, String defaultValue) ~~~ ~~~ //創建集合對象 Properties prop = new Properties(); //添加元素到集合 //prop.put(key, value); prop.setProperty("周迅", "張學友"); prop.setProperty("李小璐", "賈乃亮"); prop.setProperty("楊冪", "劉愷威"); //System.out.println(prop);//測試的使用 //遍歷集合 Set<String> keys = prop.stringPropertyNames(); for (String key : keys) { //通過鍵 找值 //prop.get(key) String value = prop.getProperty(key); System.out.println(key+"==" +value); } ~~~ # 注釋 `加 #` ## 將集合中內容存儲到文件 需求:使用Properties集合,完成把集合內容存儲到IO流所對應文件中的操作 分析: 1. 創建Properties集合 2. 添加元素到集合 3. 創建流 4. 把集合中的數據存儲到流所對應的文件中 ~~~ stroe(Writer,comments) store(OutputStream,commonts) ~~~ 把集合中的數據,保存到指定的流所對應的文件中,參數commonts代表對描述信息 5. 關閉流 ~~~ //1. 創建Properties集合 Properties prop = new Properties(); //2. 添加元素到集合 prop.setProperty("周迅", "張學友"); prop.setProperty("李小璐", "賈乃亮"); prop.setProperty("楊冪", "劉愷威"); //3. 創建流 FileWriter out = new FileWriter("prop.properties"); //4. 把集合中的數據存儲到流所對應的文件中 // save data會形成阻塞 prop.store(out, "save data"); //5. 關閉流 out.close(); ~~~ ## 讀取文件中的數據,并保存到集合 需求:從屬性集文件prop.properties 中取出數據,保存到集合中 分析: 1,創建集合 2,創建流對象 3,把流所對應文件中的數據 讀取到集合中 load(InputStream) 把指定流所對應的文件中的數據,讀取出來,保存到Propertie集合中 load(Reader) 4,關閉流 5,顯示集合中的數據 ~~~ public static void main(String[] args) throws IOException { //1,創建集合 Properties prop = new Properties(); //2,創建流對象 FileInputStream in = new FileInputStream("prop.properties"); //FileReader in = new FileReader("prop.properties"); //3,把流所對應文件中的數據 讀取到集合中 prop.load(in); //4,關閉流 in.close(); //5,顯示集合中的數據 System.out.println(prop); } ~~~ # ResourceBundle 工具 ~~~ //jdk提供的工具類加載properties文件,名字db.properties的后綴可以省略 ResourceBundle bundle = ResourceBundle.getBundle("db"); //通過key獲得需要的值 String driver = bundle.getString("driver"); System.out.println(driver); ~~~ 1. 只能加載 classes 下面的資源文件 2. 只能讀取 .properties文件 ## 國際化 后綴可以省略 根據當前語言環境取出 ~~~ public static final ResourceBundle getBundle(String baseName); ~~~ 設置語言環境 ~~~ public static final ResourceBundle getBundle(String baseName, Locale locale); ~~~ java.text是專門負責國際化的包,這個包里面有一個專門處理占位數據的操作類: MessageFormat; 格式化文本 ~~~ public static String format(String patten, Object... arguments); ~~~ 修改properties文件 ~~~ wel.msg = 歡迎{0}光臨,現在時間是:{1}! ~~~ 例子 ~~~ ResourceBundle db = ResourceBundle.getBundle("db"); String str = db.getString("wel.msg"); System.out.println(MessageFormat.format(str, "jdxia", new Date())); ~~~ 國際化的程序可以根據國家不同顯示不同內容,這時候需要Locale類幫忙了 Locale保存的是一個國家的區域和編碼 * 中國: zh_CN * 美國: en_US 可以在定義資源的文件的時候加上指定語言編碼 定義中文的資源文件: db_zh_CN.properties 所有的語言代碼由Locale類設置,在Local類中 ~~~ * 構造方法: public Locale(String language, String country); * 取得當前語言環境: public static Locale getDefault(); ~~~ 例子 ~~~ Locale loc = new Locale("en", "US"); ResourceBundle db = ResourceBundle.getBundle("db", loc); String str = db.getString("wel.msg"); System.out.println(str); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看