jdbc.properties的内容
username=root
password=123456
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8
下边我删掉了一些
测试用例如下
使用java.util.Properties类的load()方法
/**
* @ClassName: PropertiesTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author: maqiang 13032946420@163.com
* @date 2018年5月12日 上午10:19:08
*
*/
class PropertiesTest {
private final String dbConnFile = "src/main/resources/jdbc.properties";
@Test
public void loadConnProPerties() throws Exception, IOException {
/*1。使用java.util.Properties类的load()方法
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
API: load(InputStream) / store(OutputStream, String)方法
Properties p = new Properties();
p.load(in);*/
Properties props= new Properties();
props.load(new FileInputStream(dbConnFile));
System.out.println(props);
System.out.println(props.getProperty("driver"));
}
}
测试结果:
{url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8, druid.pool.size.min=3, jdbc.password=123456, username=root, druid.pool.size.max=20, jdbc.url=jdbc:mysql://localhost:3306/ttms?useUnicode=true&characterEncoding=utf-8, jdbc.driverClassName=com.mysql.jdbc.Driver, druid.pool.size.init=3, jdbc.username=root, password=123456, driver=com.mysql.jdbc.Driver}
com.mysql.jdbc.Driver
等价代码
class PropertiesTest {
private final String dbConnFile = "src/main/resources/jdbc.properties";
@Test
public void loadConnProPerties() throws Exception, IOException {
/*1。使用java.util.Properties类的load()方法
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
使用了转化流
API: load(InputStream) / store(OutputStream, String)方法
Properties p = new Properties();
p.load(in);*/
Properties props= new Properties();
props.load(new BufferedInputStream(new FileInputStream(dbConnFile)));
System.out.println(props);
System.out.println(props.getProperty("driver"));
}
}
使用class变量的getResourceAsStream()方法
class PropertiesTest {
private final String dbConnFile = "src/main/resources/jdbc.properties";
private final String dbConnFiletwo ="/main/resources/jdbc.properties";
@Test
public void loadConnProPerties() throws Exception, IOException {
/*使用class变量的getResourceAsStream()方法
示例: InputStream in = this.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);*/
Properties props= new Properties();
InputStream inputStream = PropertiesTest.class.getResourceAsStream(dbConnFiletwo);
props.load(inputStream);
System.out.println(props);
System.out.println(props.getProperty("driver"));
}
}
运行结果如上
使用java.util.ResourceBundle类的getBundle()方法
class PropertiesTest {
private final String dbConnFile = "src/main/resources/jdbc.properties";
private final String dbConnFiletwo ="/main/resources/jdbc.properties";
private final String dbConnFilethree ="main/resources/jdbc";
@Test
public void loadConnProPerties() throws Exception, IOException {
/*
* 使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
*/
ResourceBundle rb = ResourceBundle.getBundle(dbConnFilethree);
System.out.println(rb.getString("driver"));
System.out.println(rb);
}
}
运行结果如上所示
使用class.getClassLoader()
class PropertiesTest {
private final String dbConnFile = "src/main/resources/jdbc.properties";
private final String dbConnFiletwo ="/main/resources/jdbc.properties";
private final String dbConnFilethree ="main/resources/jdbc";
private final String dbConnFilefour ="main/resources/jdbc.properties";
@Test
public void loadConnProPerties() throws Exception, IOException {
/*
*使使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
*/
InputStream inStream = PropertiesTest.class.getClassLoader().getResourceAsStream(dbConnFilefour);
Properties properties = new Properties();
properties.load(inStream);
System.out.println(properties.getProperty("driver"));
System.out.println(properties);
}
}
使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
class PropertiesTest {
private final String dbConnFile = "src/main/resources/jdbc.properties";
private final String dbConnFiletwo ="/main/resources/jdbc.properties";
private final String dbConnFilethree ="main/resources/jdbc";
private final String dbConnFilefour ="main/resources/jdbc.properties";
@Test
public void loadConnProPerties() throws Exception, IOException {
/*
*使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
*/
InputStream inStream =ClassLoader.getSystemResourceAsStream(dbConnFilefour);
Properties properties = new Properties();
properties.load(inStream);
System.out.println(properties.getProperty("driver"));
System.out.println(properties);
}