java 中properties 類讀取k-v設定檔

來源:互聯網
上載者:User

標籤:localhost   main   leo   bin   ext   int   closed   oca   utf-8   

properties 讀取的設定檔key和values都是string 類型

package com.bjsxt.others.pro;import java.util.Properties;/** * Properties 資源設定檔的讀寫 * 1、key 與value 只能為字串 * 2、儲存與讀取 * setProperty(String key, String value)  * getProperty(String key, String defaultValue)   * @author Administrator * */public class Demo01 {    /**     * @param args     */    public static void main(String[] args) {        //建立對象        Properties pro =new Properties();        //儲存        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");        //pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");        pro.setProperty("user", "scott");        pro.setProperty("pwd", "tiger");                //擷取        String url =pro.getProperty("url","test");        System.out.println(url);    }}
demo1
package com.bjsxt.others.pro;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Properties;/** * 使用Properties 輸出到檔案 * 資源設定檔: *  * 1、.properties * store(OutputStream out, String comments)     store(Writer writer, String comments)    2、.xml   storeToXML(OutputStream os, String comment)  :UTF-8字元集   storeToXML(OutputStream os, String comment, String encoding)      * @author Administrator * */public class Demo02 {    /**     * @param args     * @throws IOException      * @throws FileNotFoundException      */    public static void main(String[] args) throws FileNotFoundException, IOException {        //建立對象        Properties pro =new Properties();        //儲存        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");        pro.setProperty("user", "scott");        pro.setProperty("pwd", "tiger");                //儲存到e:/others  絕對路徑  盤符:        //pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");        //pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");        //使用相對路徑 當前的工程//        pro.store(new FileOutputStream(new File("db.properties")), "db配置");//        pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");        pro.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置");    }}
demo2
package com.bjsxt.others.pro;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Properties;/** * 使用Properties讀取設定檔 * 資源設定檔: * 使用相對與絕對路徑讀取 * load(InputStream inStream)    load(Reader reader)    loadFromXML(InputStream in)  * @author Administrator * */public class Demo03 {    /**     * @param args     * @throws IOException      * @throws FileNotFoundException      */    public static void main(String[] args) throws FileNotFoundException, IOException {        Properties pro=new Properties();        //讀取 絕對路徑        //pro.load(new FileReader("e:/others/db.properties"));        //讀取 相對路徑        pro.load(new FileReader("src/com/bjsxt/others/pro/db.properties"));        System.out.println(pro.getProperty("user", "bjsxt"));    }}
demo3
package com.bjsxt.others.pro;import java.io.IOException;import java.util.Properties;/** * 使用類相對路徑讀取設定檔 *  bin   * @author Administrator * */public class Demo04 {    /**     * @param args     * @throws IOException      */    public static void main(String[] args) throws IOException {        Properties pro =new Properties();        //類相對路徑的 / bin         //pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));        //"" bin         pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));        System.out.println(pro.getProperty("user", "bjsxt"));    }}
demo4

 

java 中properties 類讀取k-v設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.