Java读取.properties配置文件方法示例
作者:黄浩# 发布时间:2023-08-24 16:32:56
一、介绍
Properties文件在Java中主要为配置文件,文件类型为:.properties,格式为文本文件,内容格式为"键=值"
二、读取
这里我采用的是getResourceAsStream的文件读取方法
如果想要使用这个方法,则需要了解一些基本使用信息:
1、读取文件路径范围:只局限于工程的源文件中
2、文件访问形式:带"/"是绝对路径,不带"/"是相对路径
3、读取文件类型:主要为:.properties文件,.xml文件
三、使用
主要方法有:
1、 load ( InputStream inStream) :从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如的 beans.properties 文件)进行装载来获取该文件中的所有键 - 值对。
2、 setProperty ( String key, String value) :调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
3、 getProperty ( String key) :用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
4、 store ( OutputStream out, String comments) :以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5、 clear ():清除所有装载的 键 - 值对。该方法在基类中提供。
java项目配置文件存放位置:
Maven项目配置文件存放位置:
配置文件:
className = edu.nf.ch02.impl.Sub
java代码:
public class Main {
public static void main(String[] args) throws IOException {
//创建Properties对象
Properties prop = new Properties();
//读取classPath中的properties文件
prop.load(Main.class.getClassLoader().getResourceAsStream("bean.properties"));
//根据键取出值
String className = prop.getProperty("className");
System.out.println(className);
}
}
运行结果:
封装的PropertiesUtil工具类:
public class PropertyUtil {
private static Properties prop = new Properties();
static {
try {
prop.load(PropertyUtil.class.getClassLoader().getResourceAsStream("calculator.properties"));
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
/**
* 根据Name获取Property
* @param name
* @return
*/
public static String getProperty(String name) {
return prop.getProperty(name);
}
/**
* 获取所有的Property
* @return
*/
public static List<String> getBeanFactoryClass() {
List<String> list = new ArrayList<>();
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
list.add(prop.getProperty(key));
}
return list;
}
}
以上所述是小编给大家介绍的Java读取.properties配置文件详解整合,希望对大家有所帮助
来源:https://www.cnblogs.com/hhmm99/p/9803119.html
猜你喜欢
- 先对Spring SpringMVC和Mybatis单独进行配置,最后对三者进行整合配置Spring实际使用中,一般会使用注解+xml配置来
- 目录事件最基本的用法理解路由事件WPF中使用路由事件升级了传统应用开发中的事件,在WPF中使用路由事件能更好的处理事件相关的逻辑,我们从这篇
- java沙箱环境测试支付宝支付接口?准备工作,登陆支付宝开放平台,进入沙箱环境开放平台链接:https://developers.alipa
- 这篇文章主要介绍了Spring MVC处理方法返回值过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 引言在App日益追求体验的时代,优秀的用户体验往往会使产品脱颖而出。今天我们就来介绍一种简单的滑动ListView来显示或者隐藏ToolBa
- 作者:sparkdev出处:http://www.cnblogs.com/sparkdev/注意,本文所说的断点续传特指 HTTP 协议中的
- 本文实例讲述了android动态布局之动态加入TextView和ListView的方法。分享给大家供大家参考。具体实现方法如下:packag
- 我就废话不多说了,大家还是直接看代码吧~private LineRenderer line1; &
- 本文实例为大家分享了Android实现密码明密文切换的具体代码,供大家参考,具体内容如下小眼睛在密码栏右边!奉上我使用的素材:添加图片到re
- 本文实例讲述了Android编程实现二维码的生成与解析。分享给大家供大家参考,具体如下:直接上代码,代码上面有具体的解析,并且提供jar供下
- 什么是死锁我们先看看这样一个生活中的例子:在一条河上有一座桥,桥面较窄,只能容纳一辆汽车通过,无法让两辆汽车并行。如果有两辆汽车A和B分别由
- import java.util.regex.Matcher;import java.util.regex.Pattern; /*
- 经过数字签名的文档,能够使作者之外的人无法对其进行修改。因此,在PDF文档中添加数字签名可以保证其安全性和真实性。同时根据添加内容的差异性,
- 1. Handler使用引出现在作为客户,有这样一个需求,当打开Activity界面时,开始倒计时,倒计时结束后跳转新的界面(思维活跃的朋友
- 导语如下图所示,这是一个导航选择弹框。进行单项选择,然后会监听回调选择的事件。问题是Android的RadioButton是一般是放在Rad
- 先看下效果图:这个需要用到1个开源的 库,这个后面也会说下的。工程目录:1. MainActivity.javapublic class M
- 一、API简介Thread.sleep()是Thread类的一个静态方法,使当前线程休眠,进入阻塞状态(暂停执行),如果线程在睡眠状态被中断
- 页面代码:<%@ page language="java" contentType="text/html
- Sentinel 是什么随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统
- 1 什么是条件变量condition_variable是一个类,常和mutex搭配使用。condition_variable类是一个同步原语