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"));
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/16805.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【深度学习】计算机视觉(13)——模型评价及结果记录

1 Tensorboard怎么解读? 因为意识到tensorboard的使用远不止画个图放个图片那么简单,所以这里总结一些关键知识的笔记。由于时间问题,我先学习目前使用最多的功能,大部分源码都包含summary的具体使用,基本不需要自己修…

找高清无水印视频素材,就上这9个网站。

推荐几个我的视频素材库,有免费、收费、商用,希望对大家有帮助! 1、菜鸟图库 https://www.sucai999.com/video.html?vNTYwNDUx 菜鸟图库可以找到设计、办公、图片、视频、音频等各种素材。视频素材就有上千个,全部都很高清&…

unityt光线透射目标

介绍 在Unity中,光线透射目标通常指的是在场景中放置的一些物体,用于模拟光线从一个物体透过到另一个物体的效果。canvas子物体组件中,勾不勾选“光线透射目标”有什么区别? 方法 在Canvas子物体组件中勾选“光线透射目标”时&…

Python基础合集 练习17(类与对象)

class Dog: pass papiDog() print(papi) print(type(papi)) 构建方法 创建类过后可以定义一个特殊的方法。在python中构建方法是__init__(),init()必须包含一个self参数 class pig(): #def__init__(self) -> None: print(‘你好’) pipgpig() 属性和方法 cl…

C++好难(2):类和对象(上篇)

okay,从这里开始,就进入c比较难的部分了~啊啊啊!!! (﹃ԅ) 坚持坚持啦 ~ ᵎ(•̀㉨•́)و ̑̑ 【本章目标】 1.面向过程和面向对象初步认识 2.类的引入 3.类的定义 4.类的访问限定符及封装 5.类的作用域 6.类的实…

(1)QT基础铺垫

目录 1.Qt特性 2. 新建项目 3. 工作目录与构建目录 4. 工作目录 4.1 .pro 项目配置文件 4.2 dialog.h 4.3 dialog.cpp 4.4 main.cpp 5. 帮助文档 6. 调试信息 1.Qt特性 Qt经常被当作是一个基于c语言的gui开发框架,但是这并不是qt的全部,除了开…

JavaWeb( 二 ) URL

1.4.URL统一资源定位符 URL代表Uniform Resource Locator 统一资源定位符,也叫 URL地址 。是用于标识和定位Web上资源的地址,通常用于在Web浏览器中访问网站和文件。 URL由若干部分组成,scheme:// host : port / path 例如: htt…

WxGL应用实例:绘制点云

WxGL附带了几个工具函数,其中read_pcfile用来解析.ply和.pcd格式的点云文件,该函数返回一个PointCloudData类实例,包含以下属性: PointCloudData.ok - 数据是否可用,布尔型PointCloudData.info - 数据可用性说明&…

《通过十几轮数据进行模型训练,实现精确的无创血糖测量的演绎学习》阅读笔记

目录 0 演绎学习 1 论文摘要 2 论文十问 3 论文亮点与不足之处 4 与其他研究的比较 5 实际应用与影响 6 个人思考与启示 参考文献 0 演绎学习 在本文中,DL指的是Deduction Learning,即演绎学习方法。该方法是一种机器学习方法,通过使…

简单毛概刷题网页制作 3.0(拖欠近一年版)

原因是大概一年之前学校的毛概期末刷题网站突然崩了,但是一直没有修复。当时眼看着复习时间逐渐被压缩,自己啥也做不了,遂自学前端完成毛概刷题网页一枚。 最早的毛概刷题网站仅仅是 1.0 版本(传送门),功能…

STM32F4_USMART调试组件

目录 1. USMART是什么? 2. USMART的特点 3. USMART实现流程 4. USMART组件 5. 在usmart_config.c中添加想要被USMART调用的函数 6. 实验程序 6.1 main.c 6.2 usmart.c 6.3 usmart.h 7. USMART调试的优越性说明 1. USMART是什么? USMART 是 AL…

org.apache.poi 设置 Excel 单元格颜色 RGB

一、背景说明 在使用 org.apache.poi 导出 Excel 时,需要设置部分单元格的颜色。 可以使用方法:org.apache.poi.ss.usermodel.CellStyle.setFillForegroundColor() 和 org.apache.poi.ss.usermodel.CellStyle.setFillPattern() 来设置单元格的颜色和填…

低频量化之 可转债 配债数据及策略 - 全网独家

目录 历史文章可转债配债数据 待发转债(进展统计)待发转债(行业统计)待发转债(5证监会通过,PE排序)待发转债(5证监会通过,安全垫排序)待发转债(5证…

【算法】一文彻底搞懂ZAB算法

文章目录 什么是ZAB 算法?深入ZAB算法1. 消息广播两阶段提交ZAB消息广播过程 2. 崩溃恢复选举参数选举流程 ZAB算法需要解决的两大问题1. 已经被处理的消息不能丢2. 被丢弃的消息不能再次出现 最近需要设计一个分布式系统,需要一个中间件来存储共享的信息…

Java 怎样实现代理模式,有什么优缺点

一、介绍 代理模式是一种常见的设计模式,它可以为其他对象提供一种代理以控制对这个对象的访问。代理对象具有与被代理对象相同的接口,客户端无需知道代理对象和被代理对象的区别。代理模式可以应用于各种不同的场景,例如远程代理、虚拟代理…

SpringBoot整合Mybatis-plus实现多级评论

在本文中,我们将介绍如何使用SpringBoot整合Mybatis-plus实现多级评论功能。同时,本文还将提供数据库的设计和详细的后端代码,前端界面使用Vue2。 数据库设计 本文的多级评论功能将采用MySQL数据库实现,下面是数据库的设计&…

vcruntime140.dll无法继续执行代码?vcruntime140.dll如何修复?只需要3步即可

vcruntime140.dll是用于Microsoft Visual C Redistributable(可再发行组件)的一部分,它是一个动态链接库文件,包含了该软件包提供的运行库。在许多应用程序和游戏中,vcruntime140.dll文件经常被使用。如果该文件缺失或…

spark 数据的加载和保存(Parquet、JSON、CSV、MySql)

spark数据的加载和保存 SparkSQL 默认读取和保存的文件格式为 parquet 1.加载数据 spark.read.load 是加载数据的通用方法 scala> spark.read. csv format jdbc json load option options orc parquet schema table text textFile 如果读取不同格式的数据,可以…

后端要一次性返回我10万条数据

问题描述 面试官:后端一次性返回10万条数据给你,你如何处理?我:歪嘴一笑,what the f**k! 问题考察点 看似无厘头的问题,实际上考查候选人知识的广度和深度,虽然在工作中这种情况很少遇到... …

情景剧本杀闯关系统

情景剧本杀闯关软件的开发需求通常包括以下几个方面: 剧本设计:开发者需要根据用户需求和市场调研,设计不同主题和难度等级的剧本内容,以及游戏过程中的任务、角色和道具等。 游戏引擎开发:为了实现游戏过程中…
最新文章