zoukankan      html  css  js  c++  java
  • 使用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);
            
        }
    }
  • 相关阅读:
    MSSQL '20210806'转换成'2021-08-06'
    cxgrid 列内容居中显示
    CXGRID 导出EXCEL
    study PostgreSQL【3-get数据库中all表以及表的字段信息】
    study PostgreSQL【2-FireDAC连接PostgreSQL】
    高格-销售发票勾稽销售出货的赠品处理【14】
    study PostgreSQL【1-PostgreSQL对象】
    高格-负库存导致系统异常的处理【13】
    study Rust-9【组织管理】
    基础资料属性不符合目标组织要求:物料.允许库存,物料.来料检验
  • 原文地址:https://www.cnblogs.com/liuhongfeng/p/4176224.html
Copyright © 2011-2022 走看看