使用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);
        
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值