SpringMVC(11):利用数据流InputStream 读取调用 database.properties 配置文件的内容及实例

18/1/15

下面列出 数据流InputStream 读取 database.properties 配置文件内容的步骤:

1、创建 Properties 对象:

Properties properties = new Properties();

2、创建 InputStream 对象:

(1)第一种方法:

		//one
		String configFile = "database.properties";
		InputStream is = test5.class.getResourceAsStream("/"+configFile);

(2) 第二种方法:

		//two
		InputStream is = this.getClass().getResourceAsStream("/database.properties");

3、用Java.util包里的Properties类的 load()方法来加载一个Properties配置文件:

                properties.load(is);//stream of properties

4、用 Properties类的getProperty (key)方法获取配置文件内容的键值对:

		String driver = properties.getProperty("jdbc.driver");
		String url = properties.getProperty("jdbc.url");
		String username = properties.getProperty("jdbc.username");
		String password = properties.getProperty("jdbc.password");
		System.out.println("driver+url+username+password: "+driver+url+username+password);

5、关闭数据流:

		try {
			is.close();
		}catch(IOException e){
			e.printStackTrace();
		}

6、database.properties 配置文件内容如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=

7、test5.java 单元测试如下:

package test01;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.junit.Test;

public class test5 {
	@Test
	public void pp() throws IOException{
		Properties properties = new Properties();

		//two
//		InputStream is = this.getClass().getResourceAsStream("/database.properties");
		//one
		String configFile = "database.properties";
		InputStream is = test5.class.getResourceAsStream("/"+configFile);
		
		properties.load(is);//stream of properties
		String driver = properties.getProperty("jdbc.driver");
		String url = properties.getProperty("jdbc.url");
		String username = properties.getProperty("jdbc.username");
		String password = properties.getProperty("jdbc.password");
		System.out.println("driver+url+username+password: "+driver+url+username+password);
		try {
			is.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}

8、输出结果:

driver+url+username+password: com.mysql.jdbc.Driverjdbc:mysql://localhost:3306/testroot

9、注意之处(其他博客精华):

1. //可以指定绝对路径

InputStream is=new FileInputStream("lib/config.properties"); 


2. //可以直接放在src下面读取到的路径就是web-inf下面的classes相对路径

InputStream is = this.getClass().getResourceAsStream("/config.properties");


      2.1 资源配置文件在classes下

        InputStream in = this.class.getClassLoader().getResourceAsStream("config.properties");

    注意事项:如上以'/ '开头的是指从根目录开始加载。


      2.2  使用类加载器的方式

      InputStream in = Main.class.getClassLoader().getResourceAsStream("test/resource/config.properties");

      指定加载资源配置文件的classes相对路径

      InputStream in = Main.class.getResourceAsStream("/test/resource/config.properties"); 

eg:

Properties prop = new Properties();

InputStream is = null;
is=new FileInputStream("lib/config.properties");
prop.load(is);
Class.forName(prop.getProperty("driverifx"));
Class.forName(prop.getProperty("driveroracle"));

this.oracleConn = DriverManager.getConnection(prop.getProperty("urloracle"), prop.getProperty("usernameoracle"), prop.getProperty("userpwdoracle"));
if (this.oracleConn != null) {
     logger.info("CONNECT ORACLE SUCCESS");
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值