Java—读取properties配置文件

news/2024/10/18 2:35:51/

编写配置文件

java">username=root
password=123456
url=jdbc:mysql://localhost:3306/myDatabase
driverClassName=com.mysql.cj.jdbc.Driver

编写测试类

java">import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;/*** properties配置文件的读取*/
public class FileProperties {public static void main(String[] args) {// 配置类对象Properties properties = new Properties();try (// 创建输入流FileInputStream fis = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("jdbc.properties").getPath())) {// 加载流文件properties.load(fis);// 获取配置文件的内容/* System.out.println("username: ==>" + properties.getProperty("username"));System.out.println("password: ==>" + properties.getProperty("password"));System.out.println("url: ==>" + properties.getProperty("url"));System.out.println("driverClassName: ==>" + properties.getProperty("driverClassName"));*/Enumeration<?> enumeration = properties.propertyNames();while (enumeration.hasMoreElements()) {String key = (String) enumeration.nextElement();String value = properties.getProperty(key);System.out.println(key + "==>" + value);}} catch (IOException e) {throw new RuntimeException(e);}}
}

结果

使用JDK提供的工具类ResourceBundle(资源绑定类)

java">import java.util.Enumeration;
import java.util.ResourceBundle;public class ResourceBundleTest {public static void main(String[] args) {// 相对路径是类的根目录src,例如:jdbc.properties 的完整路劲=》 src/com/java/study/jdbc.properties/*  使用:不需要.propertiesResourceBundle.getBundle("com.java.study.jdbc");ResourceBundle.getBundle("com/java/study/jdbc");*/// 获取资源绑定器对象,这里我的jdbc.properties 就在src下,所以直接写ResourceBundle bundle = ResourceBundle.getBundle("jdbc");// 通过key获取value/*System.out.println(bundle.getString("username"));System.out.println(bundle.getString("password"));System.out.println(bundle.getString("url"));System.out.println(bundle.getString("driverClassName"));*/Enumeration<String> keys = bundle.getKeys();while (keys.hasMoreElements()) {String key = keys.nextElement();String value = bundle.getString(key);System.out.println(key + "=" + value);}}
}


http://www.ppmy.cn/news/1469504.html

相关文章

【React】React 的useDebugValue作用是什么,怎么使用

React的useDebugValue是一个Hook,它主要用于在开发过程中帮助开发者调试自定义Hook。它的主要作用是将自定义Hook中的某些值暴露给React开发工具(如React DevTools),以便于开发者在调试时能够更直观地查看和理解组件的状态。 useDebugValue的作用: 调试自定义Hook:useDe…

2024050802-重学 Java 设计模式《实战模板模式》

重学 Java 设计模式&#xff1a;实战模版模式「模拟爬虫各类电商商品&#xff0c;生成营销推广海报场景」 一、前言 黎明前的坚守&#xff0c;的住吗&#xff1f; 有人举过这样一个例子&#xff0c;先给你张北大的录取通知书&#xff0c;但要求你每天5点起床&#xff0c;12点…

Rust anyhow 简明教程

anyhow 是 Rust 中的一个库&#xff0c;旨在提供灵活的、具体的错误处理能力&#xff0c;建立在 std::error::Error 基础上。它主要用于那些需要简单错误处理的应用程序和原型开发中&#xff0c;尤其是在错误类型不需要被严格区分的场景下。 以下是 anyhow 的几个关键特性&…

A45 STM32_HAL库函数 之 SMARTCARD通用驱动 -- A -- 所有函数的介绍及使用

A45 STM32_HAL库函数 之 SMARTCARD通用驱动 -- A -- 所有函数的介绍及使用 1 该驱动函数预览1.1 HAL_SMARTCARD_Init1.2 HAL_SMARTCARD_DeInit1.3 HAL_SMARTCARD_MspInit1.4 HAL_SMARTCARD_MspDeInit1.5 HAL_SMARTCARD_ReInit1.6 HAL_SMARTCARD_Transmit1.7 HAL_SMARTCARD_Rece…

46-1 护网溯源 - 钓鱼邮件溯源

一、客户提供钓鱼邮件样本 二、行为分析 三、样本分析 对钓鱼邮件中的木马程序1111.exe文件进行了分析,提交了360安全大脑沙箱云和微步在线云沙箱。 360安全大脑沙箱云显示,该1111.exe文件存在危险 建议使用360压缩软件进行解压,同时注意系统安全,避免不必要的风险。 四…

ISO七层模型 tcp/ip

OSI七层模型&#xff08;重点例子&#xff09; OSI&#xff08;Open Systems Interconnection&#xff09;模型&#xff0c;也称为开放系统互连模型&#xff0c;是一个理论模型&#xff0c;由国际标准化组织&#xff08;ISO&#xff09;制定&#xff0c;用于描述和理解不同网络…

171.二叉树:二叉树的所有路径(力扣)

代码解决 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr, right(nullptr) {}* Tree…

php实现一个简单的MySQL分页

一、案例演示&#xff1a; 二、php 代码 <?php $servername "localhost"; // MySQL服务器名称或IP地址 $username "root"; // MySQL用户名 $password "123456"; // MySQL密码 $dbname "test"; // 要连接…