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配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Java JVM运行时数据区(Run-Time Data Areas)
运行时数据区,是java虚拟机定义的在程序执行期间使用的各种运行时的数据区,通过JVM运行时数据区图例给大家展示的很详细,对JVM 运行时数据区相关知识感兴趣的朋友跟随小编一起看看吧2021-06-06springboot使用dubbo和zookeeper代码实例
这篇文章主要介绍了springboot使用dubbo和zookeeper代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-11-11Mybatis plus逻辑删除注解@TableLogic的使用
本文主要介绍了Mybatis plus逻辑删除注解@TableLogic,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-01-01
最新评论