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

 更新时间:2023年07月22日 08:54:35   作者:怡人蝶梦  
这篇文章主要给大家介绍了关于Java读取Properties配置文件的6种方式,java中的properties文件是一种配置文件,主要用于表达配置信息,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

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中类的加载顺序剖析(常用于面试题)

    Java中类的加载顺序剖析(常用于面试题)

    这篇文章主要介绍了Java中类的加载顺序剖析(常用于面试题),本文直接给出代码实例和运行结果,给后给出了加载过程总结,需要的朋友可以参考下
    2015-03-03
  • Java JVM运行时数据区(Run-Time Data Areas)

    Java JVM运行时数据区(Run-Time Data Areas)

    运行时数据区,是java虚拟机定义的在程序执行期间使用的各种运行时的数据区,通过JVM运行时数据区图例给大家展示的很详细,对JVM 运行时数据区相关知识感兴趣的朋友跟随小编一起看看吧
    2021-06-06
  • SpringMVC实现自定义类型转换器

    SpringMVC实现自定义类型转换器

    本篇文章主要介绍了SpringMVC实现自定义类型转换器 ,详细的介绍了自定义类型转换器的用法和好处,有兴趣的可以了解一下。
    2017-04-04
  • Java实现导入导出Excel文件的方法(poi,jxl)

    Java实现导入导出Excel文件的方法(poi,jxl)

    这篇文章主要介绍了Java实现导入导出Excel文件的方法(poi,jxl),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • springboot使用dubbo和zookeeper代码实例

    springboot使用dubbo和zookeeper代码实例

    这篇文章主要介绍了springboot使用dubbo和zookeeper代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • SpringMVC使用RESTful接口案例详解

    SpringMVC使用RESTful接口案例详解

    RESTful是一种web软件风格,它不是标准也不是协议,它不一定要采用,只是一种风格,它倡导的是一个资源定位(url)及资源操作的风格,这篇文章主要介绍了SpringBoot使用RESTful接口
    2022-11-11
  • spring boot高并发下耗时操作的实现方法

    spring boot高并发下耗时操作的实现方法

    这篇文章主要给大家介绍了关于spring boot高并发下耗时操作的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-11-11
  • Mybatis plus逻辑删除注解@TableLogic的使用

    Mybatis plus逻辑删除注解@TableLogic的使用

    本文主要介绍了Mybatis plus逻辑删除注解@TableLogic,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • java读取resource目录下文件的方法示例

    java读取resource目录下文件的方法示例

    这篇文章主要介绍了利用java读取resource目录下文件的方法,文中给出了详细的示例代码,相信对大家具有一定的参考借鉴,需要的朋友们下面来一起看看吧。
    2017-02-02
  • Java标识接口的使用方法

    Java标识接口的使用方法

    在本篇文章中小编给大家分享了关于Java标识接口的使用方法和教程内容,有需要的朋友们学习下。
    2019-01-01

最新评论