Java读取.properties配置文件的几种方式
作者:暴脾气大大 发布时间:2021-07-07 05:56:03
Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中。然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配置文件就比较容易。
介绍几种读取方式:
1、基于ClassLoder读取配置文件
注意:该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。
Properties properties = new Properties();
// 使用ClassLoader加载properties配置文件生成对应的输入流
InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties");
// 使用properties对象加载输入流
properties.load(in);
//获取key对应的value值
properties.getProperty(String key);
2、基于 InputStream 读取配置文件
注意:该方式的优点在于可以读取任意路径下的配置文件
Properties properties = new Properties();
// 使用InPutStream流读取properties文件
BufferedReader bufferedReader = new BufferedReader(new FileReader("E:/config.properties"));
properties.load(bufferedReader);
// 获取key对应的value值
properties.getProperty(String key);
3、通过 java.util.ResourceBundle 类来读取,这种方式比使用 Properties 要方便一些
1>通过 ResourceBundle.getBundle() 静态方法来获取(ResourceBundle是一个抽象类),这种方式来获取properties属性文件不需要加.properties后缀名,只需要文件名即可
properties.getProperty(String key);
//config为属性文件名,放在包com.test.config下,如果是放在src下,直接用config即可
ResourceBundle resource = ResourceBundle.getBundle("com/test/config/config");
String key = resource.getString("keyWord");
2>从 InputStream 中读取,获取 InputStream 的方法和上面一样,不再赘述
ResourceBundle resource = new PropertyResourceBundle(inStream);
注意:在使用中遇到的最大的问题可能是配置文件的路径问题,如果配置文件入在当前类所在的包下,那么需要使用包名限定,如:config.properties入在com.test.config包下,则要使用com/test/config/config.properties(通过Properties来获取)或com/test/config/config(通过ResourceBundle来获取);属性文件在src根目录下,则直接使用config.properties或config即可。
下面附上几种方式的测试代码,仅供参考:
package com.test.properties;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.springframework.core.io.support.PropertiesLoaderUtils;
/**
*
* @ClassName: TestProperties
* @Description: 获取配置文件信息
* @date: 2017年11月25日 上午10:56:00
* @version: 1.0.0
*/
public class TestProperties {
/**
*
* @Title: printAllProperty
* @Description: 输出所有配置信息
* @param props
* @return void
* @throws
*/
private static void printAllProperty(Properties props)
{
@SuppressWarnings("rawtypes")
Enumeration en = props.propertyNames();
while (en.hasMoreElements())
{
String key = (String) en.nextElement();
String value = props.getProperty(key);
System.out.println(key + " : " + value);
}
}
/**
* 根据key读取value
*
* @Title: getProperties_1
* @Description: 第一种方式:根据文件名使用spring中的工具类进行解析
* filePath是相对路劲,文件需在classpath目录下
* 比如:config.properties在包com.test.config下,
* 路径就是com/test/config/config.properties
*
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_1(String filePath, String keyWord){
Properties prop = null;
String value = null;
try {
// 通过Spring中的PropertiesLoaderUtils工具类进行获取
prop = PropertiesLoaderUtils.loadAllProperties(filePath);
// 根据关键字查询相应的值
value = prop.getProperty(keyWord);
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
/**
* 读取配置文件所有信息
*
* @Title: getProperties_1
* @Description: 第一种方式:根据文件名使用Spring中的工具类进行解析
* filePath是相对路劲,文件需在classpath目录下
* 比如:config.properties在包com.test.config下,
* 路径就是com/test/config/config.properties
*
* @param filePath
* @return void
* @throws
*/
public static void getProperties_1(String filePath){
Properties prop = null;
try {
// 通过Spring中的PropertiesLoaderUtils工具类进行获取
prop = PropertiesLoaderUtils.loadAllProperties(filePath);
printAllProperty(prop);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据key读取value
*
* @Title: getProperties_2
* @Description: 第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作
* 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算,
* 如:当前项目路径/config/config.properties,
* 相对路径就是config/config.properties
*
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_2(String filePath, String keyWord){
Properties prop = new Properties();
String value = null;
try {
// 通过输入缓冲流进行读取配置文件
InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加载输入流
prop.load(InputStream);
// 根据关键字获取value值
value = prop.getProperty(keyWord);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 读取配置文件所有信息
*
* @Title: getProperties_2
* @Description: 第二种方式:使用缓冲输入流读取配置文件,然后将其加载,再按需操作
* 绝对路径或相对路径, 如果是相对路径,则从当前项目下的目录开始计算,
* 如:当前项目路径/config/config.properties,
* 相对路径就是config/config.properties
*
* @param filePath
* @return void
* @throws
*/
public static void getProperties_2(String filePath){
Properties prop = new Properties();
try {
// 通过输入缓冲流进行读取配置文件
InputStream InputStream = new BufferedInputStream(new FileInputStream(new File(filePath)));
// 加载输入流
prop.load(InputStream);
printAllProperty(prop);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据key读取value
*
* @Title: getProperties_3
* @Description: 第三种方式:
* 相对路径, properties文件需在classpath目录下,
* 比如:config.properties在包com.test.config下,
* 路径就是/com/test/config/config.properties
* @param filePath
* @param keyWord
* @return
* @return String
* @throws
*/
public static String getProperties_3(String filePath, String keyWord){
Properties prop = new Properties();
String value = null;
try {
InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
value = prop.getProperty(keyWord);
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
/**
* 读取配置文件所有信息
*
* @Title: getProperties_3
* @Description: 第三种方式:
* 相对路径, properties文件需在classpath目录下,
* 比如:config.properties在包com.test.config下,
* 路径就是/com/test/config/config.properties
* @param filePath
* @return
* @throws
*/
public static void getProperties_3(String filePath){
Properties prop = new Properties();
try {
InputStream inputStream = TestProperties.class.getResourceAsStream(filePath);
prop.load(inputStream);
printAllProperty(prop);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 注意路径问题
String properties_1 = getProperties_1("com/test/config/config.properties", "wechat_appid");
System.out.println("wechat_appid = " + properties_1);
getProperties_1("com/test/config/config.properties");
System.out.println("*********************************************");
// 注意路径问题
String properties_2 = getProperties_2("configure/configure.properties", "jdbc.url");
System.out.println("jdbc.url = " + properties_2);
getProperties_2("configure/configure.properties");
System.out.println("*********************************************");
// 注意路径问题
String properties_3 = getProperties_3("/com/test/config/config.properties", "wechat_appid");
System.out.println("wechat_appid = " + properties_3);
getProperties_3("/com/test/config/config.properties");
}
}
来源:https://www.cnblogs.com/sebastian-tyd/p/7895182.html
猜你喜欢
- 类必须先定义才能使用。类是创建对象的模板,创建对象也叫类的实例化。下面通过一个简单的例子来理解Java中类的定义:public class
- 一、IDEA自带打包插件内容:此种方式可以自己选择制作胖包或者瘦包,但推荐此种方式制作瘦包。输出:输出目录在out目录下流程步骤:第一步:
- 定义可理解为 适配广泛的类型,即参数化类型,可以把类型像方法的参数那样进行传递。// 以ArrayList为示例// 泛型T可以是任意类pu
- 本文实例为大家分享了java动态导出excel压缩成zip下载的具体代码,供大家参考,具体内容如下package pack.java.io.
- 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档@Con
- 数组是一种数据结构,其声明方式如下:type[] arrayName;数组具有以下属性: 1.数组可以是
- 最近在读刘增辉老师所著的《MyBatis从入门到精通》一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不
- 1.栈和队列的共同特点是(只允许在端点处插入和删除元素)4.栈通常采用的两种存储结构是(线性存储结构和链表存储结构)5.下列关于栈的叙述正确
- 本文实例为大家分享了springboot读取application.yaml文件数据的具体代码,供大家参考,具体内容如下提示:以下是本篇文章
- 今天做了一个java对象转Map的例子,执行的时候报错了,如下:Exception in thread "main" j
- 多说无益,贴代码:/** * 校验银行卡卡号 * * @param cardId &nbs
- 前言今天看代码看到有牵扯到弱引用的东西,就先稍微补一补Java的四种引用类型吧。Java为引用类型专门定义了一个类Reference,它是引
- 一、引言使用原生的zookeeper时候会遇到watcher一次注册生效一次等情况,因此使用curatorcurator是Netflix公司
- 简介使用配置类和注解代替web.xml和SpringMVC配置文件的功能在Servlet3.0环境中,容器会在类路径中查找实现javax.s
- Java 本身就自带 JS 引擎,自从 Java 1.6 开始就支持了,愈来愈好。我对 js 比较熟悉,因此有个大胆的想法,为什么不用自带
- 本文实例为大家分享了Java实现通讯录管理系统的具体代码,供大家参考,具体内容如下一、前言我们学了这么久的知识了,光学知识不会用是一件很悲伤
- 在C#的List集合操作中,有时候需要查找到List集合中的最大值,此时可以使用List集合的扩展方法Max方法,Max方法有2种形式,一种
- 这篇文章主要介绍了Java集合使用 Iterator 删除元素,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 后台生成验证码工具方法 /* * 设置图片的背景色 */ public static v
- Intellij是进行scala开发的一个非常好用的工具,可以非常轻松查看scala源码,当然用它来开发Java也是很爽的,之前一直在用sc