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; } }

      

  • 相关阅读:
    求质数与因式分解
    利用工具解题
    我的学生信息管理系统总结
    JSP简单练习-页面重定向
    CSS学习(三)—相对定位与绝对定位
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    AVL平衡树的插入例程
    SAE搭建WordPress教程 免费建WordPress博客站
    MPMoviePlayerViewController和MPMoviePlayerController的使用
    Android日志输出工具类
  • 原文地址:https://www.cnblogs.com/wudi521/p/5559026.html
Copyright © 2011-2022 走看看