zoukankan      html  css  js  c++  java
  • java读取项目根路径下和任意磁盘位置下的properties文件

    1.读取项目根路径下的properties文件比较简单也是比较常见的一种操作。

    具体代码如下:

    package com.xuanen.util;
    
    import java.util.Properties;
    
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    
    public class PropertyUtil {
    
    	private static Properties propertie;
    
    	private static Logger logger = Logger.getLogger(PropertyUtil.class);
    	
    	
    	/**
    	 * 读取配置文件
    	 */
    	public static Properties init()
        {
            if (propertie == null)
            {
                propertie = new Properties();
                try
                {
                    propertie = PropertiesLoaderUtils.loadProperties(new ClassPathResource("common.properties"));
                }
                catch (Exception ex)
                {
                    logger.error(ex.getMessage());
                }
            }
            return propertie;
        }
    	
    	/**
    	 * 获取solr的config路径
    	 */
    	public static String getDateConfigXMLPath()
        {
            propertie = init();
            String path = propertie.getProperty("dateConfigPath");
            return path;
        }
    }
    

    2.读取磁盘上任意位置的properties文件不常见,但是也要掌握。代码如下

    public class GetSolrCreateDate
    {
        // 获取dataimport.properties索引的创建时间的配置文件
      //此处可替换成磁盘任意位置的properties文件位置 例如:E:/dataimport.properties

    private static String path = PropertyUtil.getDateConfigXMLPath().replace("data-config.xml", "") + "dataimport.properties"; private static Properties propertie; private static Logger logger = Logger.getLogger(PropertyUtil.class); /** * 读取配置文件 */ public static Properties init() { if (propertie == null) { propertie = new Properties(); try { if (FileUtils.isExcite(path)){ propertie = PropertiesLoaderUtils.loadProperties(new PathResource(path)); } } catch (Exception ex) { logger.error(ex.getMessage()); } } return propertie; } // 根据表名获取索引改变的时间 public static String getCreateDateByTableName(String tableName) { propertie = init(); String data = propertie.getProperty(tableName+".last_index_time"); if (StringUtils.isNotBlank(data)) { return data; } return null; } }

      

  • 相关阅读:
    解决Hash冲突的几种方式
    深入理解JDK8中的HashMap
    JAVA中两个int类型的变量在不借助第三个变量的情况下完成值的互换
    Feign调用时读取超时(Read timed out executing GET)解决
    windows上Jenkins安装及其配置
    windows下查看端口被占用及处理
    flutter IOS模拟器无法弹出软键盘
    Android-ION内存管理简介
    移动GPU分类/百科
    ApiGen4.1 windows安装教程
  • 原文地址:https://www.cnblogs.com/wudi521/p/5559026.html
Copyright © 2011-2022 走看看