Java读取Properties配置文件的6种方式汇总
Java读取Properties的方式
项目结构:经典的maven项目结构

配置文件1和2内容一致:
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=123456
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路径下开始查找,加上/会报空指针
十分有必要知道java中类的加载过程!!!
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"));
}总结
到此这篇关于Java读取Properties配置文件的6种方式的文章就介绍到这了,更多相关Java读取Properties配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
springboot3集成mybatis-plus报sqlSession异常的问题解决
springboot3已经发布正式版,但是在集成mybatis-plus最新版3.5.2的时候发现提示异常,本文就来介绍一下报sqlSession异常的问题解决,具有一定的参考价值,感兴趣的可以了解一下2024-02-02
JavaBean和SpringBean的区别及创建SpringBean方式
这篇文章主要介绍了JavaBean和SpringBean的区别及创建SpringBean方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-10-10
解决java.sql.SQLException:The server time zone value &apo
这篇文章主要介绍了解决java.sql.SQLException:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-03-03
SpringBoot集成ip2region实现ip白名单的代码示例
ip2region v2.0 - 是一个离线IP地址定位库和IP定位数据管理框架,10微秒级别的查询效率,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现,本文介绍了SpringBoot集成ip2region实现ip白名单的代码工程,需要的朋友可以参考下2024-08-08


最新评论