有时候,写了一个配置文件,需要知道读出来的内容对不对,我们需要测试一下,看看读出来的跟我们要的是不是一样。这里写了一个工具类,用来读取配置文件里面的内容。

一、使用Properties工具类来读取。

1.新建一个java工程,导入需要的jar包,新建一个配置文件 如下图:

2.配置文件的内容:

 driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/csdn
user=root
pwd=123456
initsize=1
maxactive=1
maxwait=5000
maxidle=1
minidle=1

3.读取配置文件工具类:

 package com.cnblogs.daliu_it;

 import java.io.FileInputStream;
import java.util.Properties; /**
* 读取配置文件
* @author daliu_it
*/
public class GetProperties {
public static void getProperties() {
try {
// java.util.Properties
/*
* Properties类用于读取properties文件 使用该类可以以类似Map的形式读取配置 文件中的内容
*
* properties文件中的内容格式类似: user=openlab 那么等号左面就是key,等号右面就是value
*/
Properties prop = new Properties(); /*
* 使用Properties去读取配置文件
*/
FileInputStream fis = new FileInputStream("config.properties");
/*
* 当通过Properties读取文件后,那么 这个流依然保持打开状态,我们应当自行 关闭。
*/
prop.load(fis);
fis.close();
System.out.println("成功加载完毕配置文件"); /*
* 当加载完毕后,就可以根据文本文件中 等号左面的内容(key)来获取等号右面的 内容(value)了
* 可以变相的把Properties看做是一个Map
*/
String driver = prop.getProperty("driver").trim();
String url = prop.getProperty("url").trim();
String user = prop.getProperty("user").trim();
String pwd = prop.getProperty("pwd").trim();
System.out.println("driver:" + driver);
System.out.println("url:" + url);
System.out.println("user:" + user);
System.out.println("pwd:" + pwd); } catch (Exception e) {
e.printStackTrace();
}
}
}

4.测试类:

 package com.daliu_it.test;

 import java.sql.SQLException;

 import org.junit.Test;

 import com.cnblogs.daliu_it.GetProperties;

 public class testCase {

     /**
* 获得配置文件
* @throws SQLException
*/
@Test
public void testgetProperties() throws SQLException {
GetProperties poperties=new GetProperties();
poperties.getProperties();
}
}

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

使用Properties去读取配置文件,并获得具体内容值的更多相关文章

  1. Java 数据类型:集合接口Map:HashTable;HashMap;IdentityHashMap;LinkedHashMap;Properties类读取配置文件;SortedMap接口和TreeMap实现类:【线程安全的ConcurrentHashMap】

    Map集合java.util.Map Map用于保存具有映射关系的数据,因此Map集合里保存着两个值,一个是用于保存Map里的key,另外一组值用于保存Map里的value.key和value都可以是 ...

  2. java properties类读取配置文件

    1.JAVA Properties类,在java.util包里,具体类是java.util.properties.Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值 ...

  3. properties + IO 读取配置文件

    1.项目目录树 2.配置文件config.properties username = sushe password = sushe url = jdbc:mysql://172.16.100.10:3 ...

  4. 【Properties文件】Java使用Properties来读取配置文件

    配置文件位置及内容 执行结果 程序代码 package Utils.ConfigFile;   import java.io.BufferedInputStream; import java.io.B ...

  5. Properties类读取配置文件

    package com.wzy.t4; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...

  6. Spring Boot读取配置文件的几种方式

    Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...

  7. ResourceBundle和properties 读取配置文件区别

    java.util.ResourceBundle 和java.util.properties 读取配置文件区别 这两个类都是读取properties格式的文件的,而Properties同时还能用来写文 ...

  8. 方法:一个简单的读取配置文件.properties的工具类 JAVA

    import java.util.ResourceBundle; public class ConfigHelper { private static ConfigHelper instance; p ...

  9. Java配置文件Properties的读取、写入与更新操作

    /** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...

随机推荐

  1. C#程序集系列12,C#编译器和CLR如何找寻程序集

    本篇体验C#编译器和CLR运行时如何查找程序集,并自定义CLR运行时查找程序集的方式. □ C#编译器和CLR运行时如何查找程序集 C#编译器在哪里?--在C:\Windows\Microsoft.N ...

  2. linux升级mysql到5.7

    原文:http://blog.csdn.net/qing_gee/article/details/41774265 Linux升级MySQL版本到5.7. 1.下载Linux的5.7版本,主要需要两个 ...

  3. Android之zip包换肤(极力推荐)

    转自:http://www.eoeandroid.com/thread-102536-1-1.html 直接上图,以图为证,哈哈 第一图为原始的皮肤:<ignore_js_op> 第二种为 ...

  4. Android.mk中引用第3方动态库

    Android.mk 文件内容: LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOC ...

  5. 常用sql001_partition by 以及 row_number()和 dense_rank()和rank()区别

    create table student ( sid varchar2(10), --学号 sname varchar2(10), --姓名 classid varchar2(10), --班级号 s ...

  6. Java基础(八):多态

    一.多态的理解: 多态是同一个行为具有多个不同表现形式或形态的能力. 多态就是同一个接口,使用不同的实例而执行不同操作,如图所示: 多态性是对象多种表现形式的体现:现实中,比如我们按下 F1 键这个动 ...

  7. IE浏览器无法直接识别input的type="hidden"问题

    原问题: <td class="formValue" id="in-checkbox"> <label class="checkbo ...

  8. IIS-网站发布之后访问HTTP 错误 403.14 - Forbidden

    这种问题一般是因为页面本身发生了错误的原因导致的,这个时候先开启[目录浏览]功能 开通目录浏览之后再重新访问,就能看到相应的错误了,再去进行相应的解决就可以了.

  9. COM结构化存储中存储对象或者流对象的命名规则

      COM结构化存储中存储对象或者流对象的命名规则

  10. [jQuery] Custom event trigger

    $(document).ready(function(){ // Get Weather $('button').on('show.weather', function() { var results ...