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配置文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot3集成mybatis-plus报sqlSession异常的问题解决

    springboot3集成mybatis-plus报sqlSession异常的问题解决

    springboot3已经发布正式版,但是在集成mybatis-plus最新版3.5.2的时候发现提示异常,本文就来介绍一下报sqlSession异常的问题解决,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • Java强制类型转换原理详解(父类转子类、子类转父类)

    Java强制类型转换原理详解(父类转子类、子类转父类)

    这篇文章主要给大家介绍了关于Java强制类型转换原理(父类转子类、子类转父类)的相关资料,所谓的强制类型转换,其实是自动类型转换的逆过程,在数据类型兼容的情况下,将容量大的数据类型转换为容量小的数据类型,需要的朋友可以参考下
    2023-12-12
  • JavaBean和SpringBean的区别及创建SpringBean方式

    JavaBean和SpringBean的区别及创建SpringBean方式

    这篇文章主要介绍了JavaBean和SpringBean的区别及创建SpringBean方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • 浅谈Spring中单例Bean是线程安全的吗

    浅谈Spring中单例Bean是线程安全的吗

    这篇文章主要介绍了浅谈Spring中单例Bean是线程安全的吗?具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • java实现桌球小游戏

    java实现桌球小游戏

    这篇文章主要为大家详细介绍了java实现桌球小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • request如何获取body的json数据

    request如何获取body的json数据

    这篇文章主要介绍了request如何获取body的json数据操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Spring Boot常见外部配置文件方式详析

    Spring Boot常见外部配置文件方式详析

    这篇文章主要给大家介绍了关于Spring Boot常见外部配置文件方式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • Gradle 6.6.1 安装配置的详细教程

    Gradle 6.6.1 安装配置的详细教程

    Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。这篇文章主要介绍了Gradle 6.6.1 安装配置的详细教程,需要的朋友可以参考下
    2020-09-09
  • 解决java.sql.SQLException:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized问题

    解决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白名单的代码示例

    SpringBoot集成ip2region实现ip白名单的代码示例

    ip2region v2.0 - 是一个离线IP地址定位库和IP定位数据管理框架,10微秒级别的查询效率,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现,本文介绍了SpringBoot集成ip2region实现ip白名单的代码工程,需要的朋友可以参考下
    2024-08-08

最新评论