/**
* 加载properties配置文件
* 使用ResourceBundler.getBundle只能加载properties文件
* @author Administrator
*
*/
public class PropertiesTest {
@Test
public void loadProperties(){
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
//获取里面的内容
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String root = bundle.getString("root");
String password = bundle.getString("password");
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, root, password);
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(driver);
}
@Test
public void loadClassProperties(){
//使用获取流的方式加载src下的文件classLoader
InputStream is = PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties prop = new Properties();
try {
prop.load(is);
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String root = prop.getProperty("root");
String password = prop.getProperty("password");
System.out.println(driver+","+url+","+root+","+password);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}