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