推荐使用spring中的工具类:

// Properties properties = PropertiesLoaderUtils.loadAllProperties("properties/LazadaStatusMapping.properties");
   Properties properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("properties/LazadaStatusMapping.properties"));


介绍几种读取方式:参考:https://www.cnblogs.com/sebastian-tyd/p/7895182.html

、基于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); 复制代码 、基于 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); 、通过 java.util.ResourceBundle 类来读取,这种方式比使用 Properties 要方便一些   >通过 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");   >从 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");
}
}

读取配置文件properties的几种方式的更多相关文章

  1. java读取配置文件常用的四种方式

    配置文件 放置在src下面 obj.properties className=com.store.order.dao.impl.OrderDaoImpl 方式一 @Test public void t ...

  2. springboot 读取 yml 配置的几种方式

    前言:在springboot 项目中一般默认的配置文件是application.properties,但是实际项目中我们一般会使用application.yml 文件,下面就介绍一下在springbo ...

  3. java读取XML文件的四种方式

    java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...

  4. PHP读取文件内容的五种方式(转载)

    php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp); php读取文件内容: -----第一种方法--- ...

  5. C++读取字符串数据的两种方式

    C++读取字符串数据的两种方式 对于同样的样例输入: ladder came tape soon leader acme RIDE lone Dreis peat ScAlE orb eye Ride ...

  6. MySQL读取配置文件的顺序、启动方式、启动原理

    一.MySQL读取配置文件的顺序 读取顺序:/etc/my.cnf > /etc/mysql/my.cnf > /usr/etc/my.cnf > ~/.my.cnf 命令验证:[r ...

  7. 160624、Spark读取数据库(Mysql)的四种方式讲解

    目前Spark支持四种方式从数据库中读取数据,这里以Mysql为例进行介绍. 一.不指定查询条件 这个方式链接MySql的函数原型是: 1 def jdbc(url: String, table: S ...

  8. 读取property配置的N种方式

    @Value注解 @Value("${ddd}") <!-- 加载配置属性文件 --> <context:property-placeholder ignore- ...

  9. spring boot项目获取application配置文件参数的两种方式

    前言:了解过spring boot这个技术的,应该知道spring boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...

随机推荐

  1. Flask--路由备忘

    路由: 路由传参route("/index/<userid>") def index(userid),参数数据类型的指定route("user/<int ...

  2. boost::asio::io_context类

    //有个疑惑: 向io_context对象中提交的任务只能被顺序化的执行. //下面这个构造函数表明可以运行多线程啊..... /** * Construct with a hint about th ...

  3. P3811 乘法逆元

    传送 乘法逆元:ax ≡ 1 (mod p),其中x为a的逆元,求模意义下的乘法逆元,通常有一下几种方法: 1.拓展欧几里得(也就是exgcd) ax ≡ 1 (mod p) ax-py=1 这就变成 ...

  4. mina2中的session

    简介 session类图 Mina每建立一个连接同时会创建一个session对象,用于保存这次读写需要用到的所有信息.从抽象类AbstractIoSession中可以看出session具有如下功能: ...

  5. VS2008--VS2013 各种版本官方下载地址

    很多刚入门的学生都不知道从哪里下载Visual studio 编译器 , 我特闲的纯手工整理了下 Visual Studio 2005 Professional 官方90天试用版 英文版:http:/ ...

  6. [UE4]AWP组合

    AWP狙击枪可以由主枪和镜头模型组合而成. 一.主枪 二.镜头组合

  7. js基本方法

    Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b ...

  8. 为什么使能RPS/RFS, 或者RSS/网卡多队列后,QPS反而下降?

    http://laoar.github.io/blog/2017/05/07/rps/ TL;DR RPS 即receive side steering,利用网卡的多队列特性,将每个核分别跟网卡的一个 ...

  9. 「NOI2018」屠龙勇士(CRT)

    /* 首先杀每条龙用到的刀是能够确定的, 然后我们便得到了许多形如 ai - x * atki | pi的方程 而且限制了x的最小值 那么exgcd解出来就好了 之后就是扩展crt合并了 因为全T调了 ...

  10. Spring MVC与注解相关的一些配置的方法

    在spring mvc中,注解是需要通过配置文件去开启的,一般简单的项目可分为两个配置文件,这里姑且叫做spring-mvc.xml与spring-context.xml.其中spring-mvc.x ...