java 如何读取properties文件
作者:Marydon 时间:2023-08-28 05:23:01
1.情景展示
将要访问的接口地址等常用的配置添加到properties文件中,比直接写到java类中的好处在于:
当我们需要修改相应配置时,直接修改properties文件,重启tomcat即可,避免了重新编译引用该配置的java文件,同时,也便于项目的维护。
方式一
通过spring的工具类PropertiesLoaderUtils来实现对properties文件的解析
所需jar包:spring的核心jar包,spring-core-版本号.jar
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.io.support.PropertiesLoaderUtils;
/**
* 借助spring读取Properties文件
* @explain Spring 提供的 PropertiesLoaderUtils
* 允许您直接通过基于类路径的文件地址加载属性资源 最大的好处就是:实时加载配置文件,修改后立即生效,不必重启
* @author Marydon
* @creationTime 2018年5月23日上午9:58:59
* @version 1.0
* @since
* @email marydon20170307@163.com
*/
public class PropertiesUtils {
/**
* 读取properties文件
* @param fileName properties文件名及所在路径
* @explain 参数说明
* 1.传递的参数不是properties类型文件,不会报错,返回的是空Map;
* 2.传递的参数是根本不存在的properties文件,也不会报错,返回的是空Map;
* 3.传递的参数可以带路径,可以正常解析到
* @return
*/
public static Map<String, String> readProperties(String fileName) {
Map<String, String> resultMap = new HashMap<String, String>();
try {
Properties props = PropertiesLoaderUtils.loadAllProperties(fileName);
for (Object key : props.keySet()) {
resultMap.put(key.toString(), props.get(key).toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return resultMap;
}
/**
* @param args
*/
public static void main(String[] args) {
// Map map = readProperties("base/web/imageInfo/fileRootDirectories.properties");
Map map = readProperties("fileRootDirectories.properties");
for (Object key : map.keySet()) {
System.out.println(key.toString() + "=" + map.get(key).toString());
}
// 打印结果
// fileRootPath=uploadFiles
}
}
这种方式的缺点在于:
每次调用都要重新解析对应的properties文件,所以,我们可以在项目启动的时候,就把该文件加载到内存中(一次加载解析,永久使用)。
方式二
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* properties文件工具类
* @description
* 在该类被加载的时候,它就会自动读取指定位置的配置文件内容并保存到静态属性中,高效且方便,一次加载,可多次使用
* @author: Marydon
* @date: 2020年07月13日 0013 16:04
*/
public class PropertyUtils_old {
private static Logger logger = LoggerFactory.getLogger(PropertyUtils_old.class);
// 待解析的properties文件名称
private static final String FILE_NAME = "bill.properties";
// 用于接收解析过properties文件
private static Properties props;
// 静态代码块:在该类被加载到内容中时,该代码块会执行
static {
// 静态代码块执行时将要执行的方法
loadPropertiesFile();
}
synchronized static private void loadPropertiesFile() {
logger.debug("开始加载properties文件内容.......");
props = new Properties();
InputStream in = null;
try {
// 方式一:通过类加载器进行获取properties文件流(不用添加路径)
in = PropertyUtils_old.class.getClassLoader().getResourceAsStream(FILE_NAME);
// 方式二:通过类进行获取properties文件流(需要加/)
// in = PropertyUtils.class.getResourceAsStream("/bill.properties");
props.load(in);
} catch (NullPointerException e) {
logger.error("bill.properties文件未找到!");
} catch (IOException e) {
logger.error("出现IOException!");
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
logger.error("bill.properties文件流关闭出现异常");
}
}
logger.info("加载properties文件内容完成...........");
logger.info("properties文件内容:" + props);
}
/*
* 获取properties文件中指定key的value
* @date: 2020年07月13日 0013 16:17
* @param: key
* @return: java.lang.String
*/
public static String getProperty(String key) {
if (null == props) {
loadPropertiesFile();
}
return props.getProperty(key);
}
/*
* 获取properties文件中指定key的value
* @date: 2020年07月13日 0013 16:17
* @param: key
* @param: defaultValue
* @return: java.lang.String
*/
public static String getProperty(String key, String defaultValue) {
if (null == props) {
loadPropertiesFile();
}
return props.getProperty(key, defaultValue);
}
}
测试
public static void main(String[] args) {
System.out.println(getProperty("bill.czInterfaceAddress"));
}
方式一也可以按照方式二的形式,来达到一次加载,永久使用的效果。
来源:https://www.cnblogs.com/Marydon20170307/p/9148783.html
标签:java,读取,properties
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java的url方式、本地方式获取json文件内容
2023-08-22 18:30:23
java实现Object转String的4种方法小结
2023-07-18 08:54:53
![](https://img.aspxhome.com/file/2023/8/121948_0s.jpg)
@Configuration与@Component作为配置类的区别详解
2023-03-09 19:50:15
Java类之间的关系图_动力节点Java学院整理
2022-07-31 23:03:46
![](https://img.aspxhome.com/file/2023/9/68569_0s.jpg)
Java 超详细讲解ThreadLocal类的使用
2021-11-13 05:58:29
![](https://img.aspxhome.com/file/2023/2/71012_0s.png)
C#验证给定字符串形式日期是否合法的方法
2021-09-15 16:00:37
Java Map接口概述和常用方法详解
2022-07-29 14:19:06
![](https://img.aspxhome.com/file/2023/8/103998_0s.png)
Android的HTTP类库Volley入门学习教程
2022-06-08 11:56:51
![](https://img.aspxhome.com/file/2023/3/139483_0s.png)
Flutter进阶之实现动画效果(三)
2023-01-26 05:12:49
![](https://img.aspxhome.com/file/2023/8/118968_0s.gif)
关于@RequestLine的使用及配置
2023-09-23 20:52:19
![](https://img.aspxhome.com/file/2023/7/114057_0s.jpg)
Android仿英语流利说取词放大控件的实现方法(附demo源码下载)
2022-07-10 23:14:18
![](https://img.aspxhome.com/file/2023/5/88685_0s.jpg)
C#多线程的相关操作讲解
2022-01-13 18:32:12
![](https://img.aspxhome.com/file/2023/5/108625_0s.png)
Java中的接口回调实例
2023-11-29 08:05:43
android 自定义控件 自定义属性详细介绍
2022-08-05 12:19:41
那些年,我还在学习C# 学习笔记
2021-11-09 07:08:35
![](https://img.aspxhome.com/file/2023/8/92968_0s.jpg)
深入学习C#多线程
2021-06-12 12:35:04
![](https://img.aspxhome.com/file/2023/2/111102_0s.png)
java实现递归文件列表的方法
2022-10-13 13:17:00
Spring Security入门demo案例
2023-07-01 18:57:30
深入解析C++编程中对设计模式中的策略模式的运用
2022-05-26 22:52:20
![](https://img.aspxhome.com/file/2023/2/127522_0s.jpg)
Spring jackson原理及基本使用方法详解
2021-10-03 08:28:18