java基礎--properties 類使用


一、Java Properties類

    Java中有個比較重要的類Properties(Java.util.Properties),主要用於讀取Java的配置文件,各種語言都有自己所支持的配置文件,配置文件中很多變量是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程序本身去修改相關的變量設置。像Python支持的配置文件是.ini文件,同樣,它也有自己讀取配置文件的類ConfigParse,方便程序員或用戶通過該類的方法來修改.ini配置文件。在Java中,其配置文件常為.properties文件,格式為文本文件,文件的內容的格式是“鍵=值”的格式,文本注釋信息可以用"#"來注釋。

Properties類繼承自Hashtable,如下:

它提供了幾個主要的方法:

1. getProperty ( String key),用指定的鍵在此屬性列表中搜索屬性。也就是通過參數 key ,得到 key 所對應的 value。

2. load ( InputStream inStream),從輸入流中讀取屬性列表(鍵和元素對)。通過對指定的文件(比如說上面的 test.properties 文件)進行裝載來獲取該文件中的所有鍵 - 值對。以供 getProperty ( String key) 來搜索。

3. setProperty ( String key, String value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設置 鍵 - 值對。

4. store ( OutputStream out, String comments),以適合使用 load 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流。與 load 方法相反,該方法將鍵 - 值對寫入到指定的文件中去。

5. clear (),清除所有裝載的 鍵 - 值對。該方法在基類中提供。

二、Hashtable 與HashMap的區別 
1、主要:Hashtable線程安全,同步,效率相對低下
HashMap 線程不安全,非同步,效率相對高
2、父類:Hashtable 是 Dictionary HashMap 是 AbstractMap
3、null: Hashtable鍵與值不能為null
HashMap 鍵最多一個null,值可以多個null
三、Properties
     1、作用:讀寫資源配置文件
  2、鍵與值只能為字符串
  3、方法:
   setProperty(String key, String value)
   getProperty(String key)
   getProperty(String key, String defaultValue)

   后綴:.properties
   store(OutputStream out, String comments)
  store(Writer writer, String comments)
  load(InputStream inStream)
 load(Reader reader)
   .xml
storeToXML(OutputStream os, String comment) :UTF-8字符集
storeToXML(OutputStream os, String comment, String encoding)
loadFromXML(InputStream in)
三、相對路徑與絕對路徑
1、絕對路徑 : 盤符: /
2、相對路徑 : 當前項目、工程

四、類路徑加載資源文件
類所在的根路徑
1、類.class.getResourceAsStream("/")
2、Thread.currentThread().getContextClassLoader().getResourceAsStream("")

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM