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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 创建高精度定时器的示例
![](https://img.aspxhome.com/file/2023/4/76434_0s.png)
Java编程中实现归并排序算法的实例教程
Java中ArrayList与顺序表的概念与使用实例
![](https://img.aspxhome.com/file/2023/2/73092_0s.png)
Android SQLite数据库加密的操作方法
![](https://img.aspxhome.com/file/2023/2/138782_0s.jpg)
Java日常练习题,每天进步一点点(24)
![](https://img.aspxhome.com/file/2023/3/96043_0s.png)
java实现静默安装apk
Android MessageQueue消息队列主要作用详解
![](https://img.aspxhome.com/file/2023/1/101821_0s.png)
C#生成带注释的dll并引用实现
![](https://img.aspxhome.com/file/2023/2/86992_0s.png)
基于JAVA文件中获取路径及WEB应用程序获取路径的方法
C++智能指针实例详解
Android 自定义对话框 showSetPwdDialog
![](https://img.aspxhome.com/file/2023/1/108331_0s.png)
Android基于注解的6.0权限动态请求框架详解
![](https://img.aspxhome.com/file/2023/0/139420_0s.gif)
C#生成MD5的函数代码
Java 批量文件压缩导出并下载到本地示例代码
Java实现导出ZIP压缩包的方法
Android6.0蓝牙出现无法扫描设备或闪退问题解决办法
![](https://img.aspxhome.com/file/2023/5/139605_0s.jpg)
Android线性布局与相对布局的实现
![](https://img.aspxhome.com/file/2023/2/138552_0s.jpg)
Android中 webView调用JS出错的解决办法
Unity EasyTouch摇杆插件使用示例详解
![](https://img.aspxhome.com/file/2023/8/77248_0s.gif)
一文带你入门SpringMVC的配置与使用
![](https://img.aspxhome.com/file/2023/7/62137_0s.png)