Java學習筆記41(Properties類),學習筆記properties

來源:互聯網
上載者:User

Java學習筆記41(Properties類),學習筆記properties

Properties可以做到集合的資料持久儲存

它是map介面的一個實作類別,可以使用map類的方法,

不過存在區別:它沒有泛型,規定鍵類型為字串

 

這個集合在以後的開發中會經常用到,比如串連資料庫等

這裡簡單介紹下基本用法,詳細用法在後面介紹

 

儲存擷取索引值對:

package demo;import java.util.Properties;import java.util.Set;public class PropertiesDemo {    public static void main(String[] args) {        function();    }        public static void function(){        Properties pro1 = new Properties();        pro1.setProperty("a", "1");        pro1.setProperty("b", "2");        pro1.setProperty("c", "3");        System.out.println(pro1);                String value = pro1.getProperty("c");        System.out.println(value);                Set<String> set = pro1.stringPropertyNames();        for(String key:set){            System.out.println(key+"="+pro1.getProperty(key));        }            }}

 

 

集合特有方法:

load方法:

把檔案中的索引值對載入到集合中

建立一個文字檔,尾碼修改為.properties,寫入索引值對

package demo;import java.io.FileReader;import java.io.IOException;import java.util.Properties;public class PropertiesDemo {    public static void main(String[] args) throws IOException {        function();    }        public static void function() throws IOException{        Properties pro1 = new Properties();        FileReader fr1 = new FileReader("d:\\pro.properties");                pro1.load(fr1);        fr1.close();        System.out.println(pro1);    }}

 

store方法:

寫入索引值對

package demo;import java.io.FileWriter;import java.io.IOException;import java.util.Properties;public class PropertiesDemo {    public static void main(String[] args) throws IOException {        function();    }        public static void function() throws IOException{        Properties pro1 = new Properties();        pro1.setProperty("name", "zhangsan");        pro1.setProperty("age", "18");        pro1.setProperty("email", "123456@qq.com");        FileWriter fWriter = new FileWriter("d:\\a.properties");        pro1.store(fWriter, "the reason");//第二個參數經常省略        fWriter.close();    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.