加载properties资源配置文件
一、Properties类加载properties资源加载src下的配置文件:Properties p=new Properties();p.load(App.class.getResourceAsStream("/db.properties"));1.1 加载src / db.propertiessrc / db.propertiesdriverClas...
·
一、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 {
//1.使用Properties类读取src下的properties配置文件
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 {
//加载src/db.properties配置
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 {
//加载src/db.properties配置
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()
//读取配置文件1
public void readProperties1() throws IOException {
//不加/会从当前包进行寻找,加上/会从src开始找
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"));
}
//读取配置文件2
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()
//读取配置文件1
public void readProperties2() throws IOException {
//不加/,若加了会为null
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"));
}
//读取配置文件2
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("config/jdbc2.properties");
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("jdbc.properties");
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("jdbc.properties");
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("jdbc");
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());
}
}
}

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)