使用Properties去讀取配置文件,並獲得具體內容值


有時候,寫了一個配置文件,需要知道讀出來的內容對不對,我們需要測試一下,看看讀出來的跟我們要的是不是一樣。這里寫了一個工具類,用來讀取配置文件里面的內容。

 

一、使用Properties工具類來讀取。

 

1.新建一個java工程,導入需要的jar包,新建一個配置文件 如下圖:

 

2.配置文件的內容:

 

1 driver=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/csdn
3 user=root
4 pwd=123456
5 initsize=1
6 maxactive=1
7 maxwait=5000
8 maxidle=1
9 minidle=1

 

3.讀取配置文件工具類:

 1 package com.cnblogs.daliu_it;
 2 
 3 import java.io.FileInputStream;
 4 import java.util.Properties;
 5 
 6 /**
 7  * 讀取配置文件
 8  * @author daliu_it
 9  */
10 public class GetProperties {
11     public static void getProperties() {
12         try {
13             // java.util.Properties
14             /*
15              * Properties類用於讀取properties文件 使用該類可以以類似Map的形式讀取配置 文件中的內容
16              * 
17              * properties文件中的內容格式類似: user=openlab 那么等號左面就是key,等號右面就是value
18              */
19             Properties prop = new Properties();
20 
21             /*
22              * 使用Properties去讀取配置文件
23              */
24             FileInputStream fis = new FileInputStream("config.properties");
25             /*
26              * 當通過Properties讀取文件后,那么 這個流依然保持打開狀態,我們應當自行 關閉。
27              */
28             prop.load(fis);
29             fis.close();
30             System.out.println("成功加載完畢配置文件");
31 
32             /*
33              * 當加載完畢后,就可以根據文本文件中 等號左面的內容(key)來獲取等號右面的 內容(value)了
34              * 可以變相的把Properties看做是一個Map
35              */
36             String driver = prop.getProperty("driver").trim();
37             String url = prop.getProperty("url").trim();
38             String user = prop.getProperty("user").trim();
39             String pwd = prop.getProperty("pwd").trim();
40             System.out.println("driver:" + driver);
41             System.out.println("url:" + url);
42             System.out.println("user:" + user);
43             System.out.println("pwd:" + pwd);
44 
45         } catch (Exception e) {
46             e.printStackTrace();
47         }
48     }
49 }

 

4.測試類:

 1 package com.daliu_it.test;
 2 
 3 import java.sql.SQLException;
 4 
 5 import org.junit.Test;
 6 
 7 import com.cnblogs.daliu_it.GetProperties;
 8 
 9 public class testCase {
10 
11     /**
12      * 獲得配置文件
13      * @throws SQLException
14      */
15     @Test
16     public void testgetProperties() throws SQLException {
17         GetProperties poperties=new GetProperties();
18         poperties.getProperties();
19     }
20 }

 

5.效果圖:

 

 

二、使用ResourceBundle類來讀取。

 

package com.souvc.redis;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * 類名: TestResourceBundle </br> 
 * 包名: com.souvc.redis 
 * 描述: 國際化資源綁定測試 </br> 
 * 開發人員:souvc </br> 
 * 創建時間: 2015-12-10 </br> 
 * 發布版本:V1.0 </br>
 */
public class TestResourceBundle {
    public static void main(String[] args) {

        ResourceBundle resb = ResourceBundle.getBundle("config",Locale.getDefault());
        String driver=resb.getString("driver");
        String url=resb.getString("url");
        String user=resb.getString("user");
        String pwd=resb.getString("pwd");
        String initsize=resb.getString("initsize");
        System.out.println(driver);
        System.out.println(url);
        System.out.println(user);
        System.out.println(pwd);
        System.out.println(initsize);
        
    }
}

 


免責聲明!

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



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