JAVA基礎(三十六)properties類基本操作,通過配置文件讀取數據庫配置

properties配置文件


properties是JAVA中的配置文件,其後綴名是.properties.它主要用來存放一些全局易修改的的常量配置。

一,properties格式.
properties的格式是key=value,一行一個鍵值對,不用分號隔開。因爲分號是具體的值。
比如:
username=root //它獲取的就是"username"的值是"root"
username=root;//它獲取的就是"username"的值是"root;"

properties的註釋是"#“在行的開頭添加一個”#“就能註釋當前行的配置,獲取添加”!“也行
比如:
#username=root
!username=root
但一般不用”!"來註釋

使用properties要知道如何獲取一個properties以及裏面的鍵值對數據。以及如何往裏面寫鍵值對.

二,properties配置文件獲取並取得其中的配置信息
以下是數據庫配置文件db.properties的配置信息

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
user=root
pwd=root

properties常用API

方法 返回值 描述
load(InputStream(Reader) in) void 從輸入流中讀取鍵值對,即加載properties.先獲取properties的輸入流對象。然後再用properties對象的load方法加載它
getProperty(String key) String 從獲取的properties對象中根據key來獲取對應配置的值,如果沒有該key返回一個null
setProperty(String key,String value) void 從獲取的properties對象中寫入一個key=value,如果有相同的key則覆蓋.
propertyNames() Enumeration<?> 返回所有鍵的枚舉
store(Writer(OutputString) writer, String comments) void 將properties對象寫入到輸出流中。使用了setProperty(key,value)後並沒有馬上就把新的鍵值對寫入到properties中.還需要調用store()方法並創建目標properties的輸出流然後傳入到store方法中。這也才能把新設置的鍵值對寫入到properties中,comments參數是註釋說明可以傳null

1,獲取db.properties
獲取properties方法有很多種。但常用的一般就是用輸入流或者類加載器來獲取

public class UtilsJDBC {

public static void propertiesFun(String userName, String pwdr) throws ClassNotFoundException, SQLException {
            /**
             * 1,先獲取目標properties的輸入流對象
             * 然後再用properties的load方法把鍵值對加載到properties對象中
             * 其中輸入流對象的路徑,可以是絕對路徑可以是相對路徑
             * 至於相對路徑就是當前根目錄下的properties.
             */
			Reader reader = new FileReader("src/db.properties");
			Properties properties = new Properties();
			properties.load(resourceAsStream);
            /**
             * 2,使用當前類的class對象的classLoder來加載properties.
             * 然後再用properties的load方法把鍵值對加載到properties對象中
             * 獲取ClassPath下的properties.至於路徑要怎麼寫取決於你項目classpath的設置了
             * 默認JAVA項目在classes下,也就是src下的文件,src下的properties就直接寫.在文件夾下就
             * 用"/"隔開,比如test下的db.properties就寫成test/properties
             * WEB項目在:WEB-INF/Classes
             */
            InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream("db.properties");
            InputStream resourceAsStream = UtilsJDBC.class.getClassLoader().getResourceAsStream("test/db.properties");
			Properties properties = new Properties();
			properties.load(resourceAsStream);
			}
			}

在這裏插入圖片描述
2,獲取鍵值對和寫入鍵值對到properties中

public class UtilsJDBC {

public static void propertiesFun(String userName, String pwdr) throws ClassNotFoundException, SQLException {
//加載properties
  InputStream resourceAsStream = UtilsJDBC.class.getClassLoader().getResourceAsStream("test/db.properties");
			Properties properties = new Properties();
			properties.load(resourceAsStream);
            /**
             * 1,根據key獲取值
             * 獲取到這些數據庫連接必要的信息後就可以進行JDBC操作了
             */
          String  driver  = properties.getProperty("driver");
          String   url = properties.getProperty("url");
          String user = properties.getProperty("user");
          String pwd = properties.getProperty("pwd");
            /**
             * 2,獲取所有鍵值對
             */
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()){
                Object key = enumeration.nextElement();
                String property = properties.getProperty(o.toString());
               System.out.println(key+"="+property);
            }
             /**
             * 3,往目標properties寫入鍵值對。
             */
           properties.setProperty("name","123456");
           Writer writer =new FileWriter("src/db.properties");
           //調用setProperty設置了鍵值對到properties中,在調用store()方法並傳入輸出流把properties的鍵值對寫
           //到目標properties文件中
            properties.store(writer,null);
 }        
 } 

在上述用getProperty根據key獲取value中。就可以獲取保存在db.properties的driver,user,name,url。這些數據庫連接信息。接着在進行JDBC操作就可以連接數據庫了。用配置文件來保存數據庫信息能讓我們以後要修改數據庫連接信息時只要修改配置文件就可以了

總結:
1,properties配置文件主要用來配置全局易修改的常量
2,properties的獲取。通過類加載器或者直接創建包含目標properties的輸入流。在調用properties對象的load方法來加載properties
3,properties的數據操作。通過基本的getProperties(String key)來根據key獲取值。通過setProperties
和包含該properties的輸出流以及store方法來把鍵值對寫入到properties

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章