zoukankan      html  css  js  c++  java
  • PropertiesUtil 获取文件属性值

    有时候不要把一些属性值写死在代码中,而是写在配置在文件中,方便更改

    PropertiesUtil工具类:读取key-value形式的配置文件,根据key获得value值 

    1、测试类

    public class Test{
    
    	private static PropertiesUtil propertiesUtil = new PropertiesUtil("file.properties");
    	//根据文件中的key获取value值
    	String value = propertiesUtil.getStringProperty("文件中的key");
    	
    }
    

      

     2、PropertiesUtil.java工具类

    package com.util;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Collection;
    import java.util.Properties;
    import java.util.Set;
    
    import org.apache.log4j.Logger;
    
    
    public class PropertiesUtil {
    	
    	private static final Logger LOGGER = Logger.getLogger(PropertiesUtil.class);
    
    	private final Properties props;
    
    	public PropertiesUtil(final Properties props) {
    		this.props = props;
    	}
    
    	public PropertiesUtil(final String propertiesFileName) {
    		final Properties properties = new Properties();
    		InputStreamReader in = null;
    		try {
    			in = new InputStreamReader(new FileInputStream(this.getClass().getResource("/").getPath()+propertiesFileName), "UTF-8");
    			/*
    			 * 获取当前工程根目录
    			 * in = new InputStreamReader(new FileInputStream(System.getProperty("user.dir") + File.separator + propertiesFileName), "UTF-8");
    			 */
    			properties.load(in);
    		} catch (final IOException ioe) {
    			LOGGER.error("Unable to read " + propertiesFileName, ioe);
    		} finally {
    			if (in != null) {
    				try {
    					in.close();
    				} catch (final IOException ioe) {
    					LOGGER.error("Unable to close " + propertiesFileName, ioe);
    				}
    			}
    		}
    		this.props = properties;
    	}
    
    	public String getStringProperty(final String name) {
    		return props.getProperty(name);
    	}
    
    	public int getIntegerProperty(final String name, final int defaultValue) {
    		String prop = props.getProperty(name);
    		if (prop != null) {
    			try {
    				return Integer.parseInt(prop);
    			} catch (final Exception ignored) {
    				return defaultValue;
    			}
    		}
    		return defaultValue;
    	}
    
    	public long getLongProperty(final String name, final long defaultValue) {
    		String prop = props.getProperty(name);
    		if (prop != null) {
    			try {
    				return Long.parseLong(prop);
    			} catch (final Exception ignored) {
    				return defaultValue;
    			}
    		}
    		return defaultValue;
    	}
    
    	public float getFloatProperty(final String name,final float defaultValue)
    	{
    		String prop = props.getProperty(name);
    		if (prop != null) {
    			try {
    				return Float.parseFloat(prop);
    			} catch (final Exception ignored) {
    				return defaultValue;
    			}
    		}
    		return defaultValue;
    	}
    	
    	public String getStringProperty(final String name, final String defaultValue) {
    		final String prop = getStringProperty(name);
    		return (prop == null) ? defaultValue : prop;
    	}
    
    	public boolean getBooleanProperty(final String name) {
    		return getBooleanProperty(name, false);
    	}
    
    	public boolean getBooleanProperty(final String name, final boolean defaultValue) {
    		final String prop = getStringProperty(name);
    		return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
    	}
    	
    	public Set<Object> keySet()
    	{
    		return props.keySet();
    	}
    	
    	public Collection<Object> values()
    	{
    		return props.values();
    	}
    
    }
    

      

  • 相关阅读:
    lintcode 最长上升连续子序列 II(二维最长上升连续序列)
    lintcode 滑动窗口的最大值(双端队列)
    windows下实现Git在局域网使用
    eclipse导入web项目之后项目中出现小红叉解决办法
    HTTP协议基础
    c++面试常用知识(sizeof计算类的大小,虚拟继承,重载,隐藏,覆盖)
    Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
    c语言表白程序代码
    矩阵的逆矩阵,伴随矩阵
    document.getElementById()与 $()区别
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/9237443.html
Copyright © 2011-2022 走看看