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



你可能感兴趣的:(java,exception,properties,String,HashMap,url)