java读取properties

自己写的一个读取properties工具类

package com.avonaco.web.excel;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PropertiesUtil {
	
	private static final Log log = LogFactory.getLog(PropertiesUtil.class);

	private static Properties properties = new Properties() ;
	
	public static final String US = "ApplicationResources.properties";          //英文

	public static final String CHINA = "ApplicationResources_zh_CN.properties"; //中文
	
	public static final String CHINA_USER = "ApplicationResourcesUserManager_zh_CN.properties" ;
	
	public static final String US_USER = "ApplicationResourcesUserManager.properties";
	/**
	 * @desc 取得全部的key和value
	 * @param propertiesName
	 * @return
	 * @author:  lic
	 * @datetime:2011-12-14 上午9:50:21
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map getProperties( String propertiesName){
		
		Map map = new HashMap(); 
		
		if ( propertiesName != null && !propertiesName.equals("") ){
			String  url = PropertiesUtil.getPropertiesPath(propertiesName);
			if( log.isDebugEnabled() ){
				log.debug(">>>>>>>>>>>>>>>>>>>获取Properties文件路径-- " + url);
			}
			
			try {
				InputStream in =new BufferedInputStream(new FileInputStream(url));
				PropertiesUtil.properties.load(in);
				
				System.out.println("1");
				  Enumeration en = PropertiesUtil.properties.propertyNames();
				   while (en.hasMoreElements()) {
					   String key = (String) en.nextElement();
					   String value = PropertiesUtil.properties.getProperty(key);
					 map.put(key, value);
				    System.out.println();
				    if( log.isDebugEnabled()){
				    	log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>  " + propertiesName + " -- : " + key  + " >>> value = " + value);
				    }
				   }
			} catch (Exception e) {
				log.debug(">>>>>>>> PropertiesUtil取得全部的key和value出错  :  " + e);
				e.printStackTrace();
			}
		}
		
		
		return map ;
	}
	
	
	
	/**
	 * @desc 根据key得到properties的value
	 * @param propertiesKey
	 * @return Map
	 * @author:  lic
	 * @datetime:2011-12-14 上午10:02:13
	 */
	@SuppressWarnings("rawtypes")
	public static String getPropertiesValue( String propertieskey , String filename){
			
			String value = "" ;
			
			if ( propertieskey != null && !propertieskey.equals("") ){
				String pkey = filename;
				String  url = PropertiesUtil.getPropertiesPath(filename);
				try {
					InputStream in =new BufferedInputStream(new FileInputStream(url));
//					this.properties.load(in);
					PropertiesUtil.properties.load(in);
					System.out.println("1");
//					  Enumeration en = properties.propertyNames();
					  Enumeration en = PropertiesUtil.properties.propertyNames();
					   while (en.hasMoreElements()) {
						   String key = (String) en.nextElement();
						   if ( key.equals(propertieskey) ){
//							   value = this.properties.getProperty(key);
							   value = PropertiesUtil.properties.getProperty(key);
						   }
					   }
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			
			return value ;
		}
		
	

	/**
	 * @desc 根据properties文件名获取路径
	 * @param propertiesKey
	 * @return Map
	 * @author:  lic
	 * @datetime:2011-12-13 下午15:53:58
	 */
	public static String getPropertiesPath( String propertiesName ){
			
			String path = "";
			if ( propertiesName != null && !propertiesName.equals("") ){
				path = PropertiesUtil.class.getClassLoader().getResource(propertiesName).toString().substring(6);
			}
			
			return path ;
	}
		
		
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值