Java properties文件读取

本文介绍如何使用Java标准库中的Properties类来创建、读取、修改以.properties为扩展名的配置文件。通过示例展示了如何加载配置文件、获取配置项值及更新配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 创建以.properties结尾的文件。如:config.properties
  • 以key=value形式保存数据。
//例子
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=user
password=password
  • 读取配置
Properties prop = new Properties();
//InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties")
InputStream in = this.getClass().getResourceAsStream("/config.properties")
//加载文件
prop.load(in);
//根据key获取value值
prop.getProperty("key");

//文件保存属性
OutputStream out = new FileOutputStream("config.properties", true);//true表示追加打开
prop.setProperty("name", "wind");
prop.store(out, "comment");
out.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值