zoukankan      html  css  js  c++  java
  • java 读取properties文件

    java 读取properties文件

    CreationTime--2018年6月7日08点19分

    Author:Marydon

    1.准备工作  

      所需jar包:spring的核心jar包,org.springframework.core-3.0.5.RELEASE.jar

    2.实现

      通过spring的工具类PropertiesLoaderUtils来实现对properties文件的解析

     1 import java.io.IOException;
     2 import java.util.HashMap;
     3 import java.util.Map;
     4 import java.util.Properties;
     5 import org.springframework.core.io.support.PropertiesLoaderUtils;
     6 
     7 /**
     8  * 借助spring读取Properties文件
     9  * @explain Spring 提供的 PropertiesLoaderUtils
    10  * 允许您直接通过基于类路径的文件地址加载属性资源 最大的好处就是:实时加载配置文件,修改后立即生效,不必重启
    11  * @author Marydon
    12  * @creationTime 2018年5月23日上午9:58:59
    13  * @version 1.0
    14  * @since
    15  * @email marydon20170307@163.com
    16  */
    17 public class PropertiesUtils {
    18 
    19     /**
    20      * 读取properties文件
    21      * @param fileName properties文件名及所在路径
    22      * @explain 参数说明
    23      * 1.传递的参数不是properties类型文件,不会报错,返回的是空Map;
    24      * 2.传递的参数是根本不存在的properties文件,也不会报错,返回的是空Map;
    25      * 3.传递的参数可以带路径,可以正常解析到
    26      * @return
    27      */
    28     public static Map<String, String> readProperties(String fileName) {
    29         Map<String, String> resultMap = new HashMap<String, String>();
    30         try {
    31             Properties props = PropertiesLoaderUtils.loadAllProperties(fileName);
    32             for (Object key : props.keySet()) {
    33                 resultMap.put(key.toString(), props.get(key).toString());
    34             }
    35         } catch (IOException e) {
    36             e.printStackTrace();
    37         }
    38         return resultMap;
    39     }
    40 
    41     /**
    42      * @param args
    43      */
    44     public static void main(String[] args) {
    45 //        Map map = readProperties("base/web/imageInfo/fileRootDirectories.properties");
    46         Map map = readProperties("fileRootDirectories.properties");
    47         for (Object key : map.keySet()) {
    48             System.out.println(key.toString() + "=" + map.get(key).toString());
    49         }
    50         // 打印结果
    51         // fileRootPath=uploadFiles
    52     }
    53 
    54 }

    3.使用场景

      将接口访问地址和文件上传保存路径封装到properties文件中,便于查看和修改。

     相关推荐:

  • 相关阅读:
    1.4redis小结--队列在抢购活动的实现思路
    1.3redis小结--配置php reids拓展
    redis小结 1-2
    redis小结 1-1
    pandas学习小记
    Python简单算法的实现
    python编码
    ThinkPHP中的__initialize()和类的构造函数__construct()
    js正则常用方法
    总结了下PHPExcel官方读取的几个例子
  • 原文地址:https://www.cnblogs.com/Marydon20170307/p/9148783.html
Copyright © 2011-2022 走看看