返回顶部
首页 > 资讯 > 后端开发 > JAVA >Java读取Properties配置文件的6种方式
  • 834
分享到

Java读取Properties配置文件的6种方式

java 2023-08-16 22:08:35 834人浏览 独家记忆
摘要

Java读取Properties的方式 项目结构:经典的Maven项目结构 配置文件1和2内容一致: jdbc.driver=com.Mysql.cj.jdbc.Driverjdbc.url=mysql://localhost:3306/

Java读取Properties的方式

项目结构:经典的Maven项目结构

在这里插入图片描述

配置文件1和2内容一致:

jdbc.driver=com.Mysql.cj.jdbc.Driverjdbc.url=mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghaijdbc.username=rootjdbc.passWord=123456

1. this.getClass().getResourceAsStream()

//读取配置文件1public 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()

//读取配置文件1public 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"));}//读取配置文件2public 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"));    }

来源地址:https://blog.csdn.net/yirenDM/article/details/130488107

--结束END--

本文标题: Java读取Properties配置文件的6种方式

本文链接: https://lsjlt.com/news/372191.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作