一、Properties类加载properties资源
加载src下的配置文件:
Properties p=new Properties();
p.load(App.class.getResourceAsStream("/db.properties"));
加载src下的配置文件(配置文件含中文):
Properties p=new Properties();
p.load(new InputStreamReader(SheetUtils.class.getResourceAsStream("/column.properties")));
1.1 加载src / db.properties
src / db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day11
password=root
username=root
maxActive=20
minIdle=3
initialSize=20
App.java
public class App {
public static void main(String[] args) throws IOException {
Properties p=new Properties();
p.load(App.class.getResourceAsStream("/db.properties"));
Enumeration<String> propertyNames = (Enumeration<String>) p.propertyNames();
while(propertyNames.hasMoreElements()) {
String key = propertyNames.nextElement();
String value=(String) p.get(key);
System.out.println(key+"="+value);
}
}
}
二、ResourceBundle类加载properties资源
ResourceBundle:
1.加载src/db.properties 配置文件
ResourceBundle rs=ResourceBundle.getBundle("db");
2.加载src/com/jsoft/demo/db.properties
ResourceBundle rs=ResourceBundle.getBundle("org.jsoft.demo.db");
3.国际化
2.1 加载src/db.properties配置
src / db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day11
password=root
username=root
maxActive=20
minIdle=3
initialSize=20
App.java
public class App {
public static void main(String[] args) throws IOException {
ResourceBundle rs=ResourceBundle.getBundle("db");
Enumeration<String> ens = rs.getKeys();
while(ens.hasMoreElements()) {
String key = ens.nextElement();
String value = rs.getString(key);
System.out.println(key+"="+value);
}
}
}
2.2 加载src/org/jsoft/demo/db.properties配置
src/org/jsoft/demo/db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day11
password=root
username=root
maxActive=20
minIdle=3
initialSize=20
App.java
public class App {
public static void main(String[] args) throws IOException {
ResourceBundle rs=ResourceBundle.getBundle("org.jsoft.demo.db");
Enumeration<String> ens = rs.getKeys();
while(ens.hasMoreElements()) {
String key = ens.nextElement();
String value = rs.getString(key);
System.out.println(key+"="+value);
}
}
}
三、Java读取Properties配置文件的6种方式汇总
1. this.getClass().getResourceAsStream()
2.当前类的加载器进行读取this.getClass().getClassLoader().getResourceAsStream()
3. ClassLoader类的static方法 getSystemResourceAsStream()
4. Spring中的 ClassPathResource读取
5. PropertyResourceBundle读取InputStream流
6.ResourceBundle.getBundle()
1. this.getClass().getResourceAsStream()
public void readProperties1() throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream("/jdbc.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
public void readProperties1() throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream("/config/jdbc2.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
2.当前类的加载器进行读取this.getClass().getClassLoader().getResourceAsStream()
public void readProperties2() throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
public void readProperties2() throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/jdbc2.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
方法1和2区别: (classpath即为target/classes 这个目录)
Class.getResourceAsStream()
从当前类所在的位置开始查找配置文件位置。
要找到jdbc.properties和jdbc2.properties必须加/从classpath下开始查找
Class.getClassLoader().getResourceAsStream()
默认就从classpath路径下开始查找,加上/会报空指针
3. ClassLoader类的static方法 getSystemResourceAsStream()
public void readProperties3() throws IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties");
Properties properties=new Properties();
properties.load(inputStream);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
4. Spring中的 ClassPathResource读取
public void readProperties4() throws IOException {
ClassPathResource resource = new ClassPathResource("config/jdbc2.properties");
Properties properties= PropertiesLoaderUtils.loadProperties(resource);
System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
5. PropertyResourceBundle读取InputStream流
public void readProperties5() throws IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/jdbc2.properties");
PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream);
System.out.println(bundle.getString("jdbc.driver"));
System.out.println(bundle.getString("jdbc.url"));
System.out.println(bundle.getString("jdbc.username"));
System.out.println(bundle.getString("jdbc.password"));
}
6.ResourceBundle.getBundle()
public void readProperties6() {
ResourceBundle bundle=ResourceBundle.getBundle("config/jdbc2");
System.out.println(bundle.getString("jdbc.driver"));
System.out.println(bundle.getString("jdbc.url"));
System.out.println(bundle.getString("jdbc.username"));
System.out.println(bundle.getString("jdbc.password"));
}
7.案例
public class JavaRead {
@Test
public void test1() throws IOException {
InputStream in = this.getClass().getResourceAsStream("/db.properties");
Properties p=new Properties();
p.load(in);
Enumeration<?> enumeration = p.propertyNames();
while(enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
}
@Test
public void test2() throws IOException {
InputStream in = this.getClass().getClassLoader().getResourceAsStream("db.properties");
Properties p=new Properties();
p.load(in);
Enumeration<?> enumeration = p.propertyNames();
while(enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
}
@Test
public void test3() throws IOException {
InputStream in = ClassLoader.getSystemResourceAsStream("db.properties");
Properties p=new Properties();
p.load(in);
Enumeration<?> enumeration = p.propertyNames();
while(enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
}
@Test
public void test4() throws IOException {
ClassPathResource resource=new ClassPathResource("db.properties");
Properties p = PropertiesLoaderUtils.loadProperties(resource);
Enumeration<?> enumeration = p.propertyNames();
while(enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
}
@Test
public void test5() throws IOException {
InputStream in = ClassLoader.getSystemResourceAsStream("db.properties");
PropertyResourceBundle p = new PropertyResourceBundle(in);
Enumeration<String> keys = p.getKeys();
while(keys.hasMoreElements()){
System.out.println(keys.nextElement());
}
}
@Test
public void test6() throws IOException {
ResourceBundle bundle= ResourceBundle.getBundle("db");
Enumeration<String> keys = bundle.getKeys();
while(keys.hasMoreElements()){
System.out.println(keys.nextElement());
}
}
}