码迷,mamicode.com
首页 > 编程语言 > 详细

java读取properties

时间:2017-07-31 14:42:04      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:logging   source   ati   while   nbsp   ble   ash   port   获取路径   

  1 package com.avonaco.web.excel;  
  2   
  3 import java.io.BufferedInputStream;  
  4 import java.io.FileInputStream;  
  5 import java.io.InputStream;  
  6 import java.util.Enumeration;  
  7 import java.util.HashMap;  
  8 import java.util.Map;  
  9 import java.util.Properties;  
 10 import org.apache.commons.logging.Log;  
 11 import org.apache.commons.logging.LogFactory;  
 12   
 13 public class PropertiesUtil {  
 14       
 15     private static final Log log = LogFactory.getLog(PropertiesUtil.class);  
 16   
 17     private static Properties properties = new Properties() ;  
 18       
 19     public static final String US = "ApplicationResources.properties";          //英文  
 20   
 21     public static final String CHINA = "ApplicationResources_zh_CN.properties"; //中文  
 22       
 23     public static final String CHINA_USER = "ApplicationResourcesUserManager_zh_CN.properties" ;  
 24       
 25     public static final String US_USER = "ApplicationResourcesUserManager.properties";  
 26     /** 
 27      * @desc 取得全部的key和value 
 28      * @param propertiesName 
 29      * @return 
 30      * @author:  lic 
 31      * @datetime:2011-12-14 上午9:50:21 
 32      */  
 33     @SuppressWarnings({ "rawtypes", "unchecked" })  
 34     public static Map getProperties( String propertiesName){  
 35           
 36         Map map = new HashMap();   
 37           
 38         if ( propertiesName != null && !propertiesName.equals("") ){  
 39             String  url = PropertiesUtil.getPropertiesPath(propertiesName);  
 40             if( log.isDebugEnabled() ){  
 41                 log.debug(">>>>>>>>>>>>>>>>>>>获取Properties文件路径-- " + url);  
 42             }  
 43               
 44             try {  
 45                 InputStream in =new BufferedInputStream(new FileInputStream(url));  
 46                 PropertiesUtil.properties.load(in);  
 47                   
 48                 System.out.println("1");  
 49                   Enumeration en = PropertiesUtil.properties.propertyNames();  
 50                    while (en.hasMoreElements()) {  
 51                        String key = (String) en.nextElement();  
 52                        String value = PropertiesUtil.properties.getProperty(key);  
 53                      map.put(key, value);  
 54                     System.out.println();  
 55                     if( log.isDebugEnabled()){  
 56                         log.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>  " + propertiesName + " -- : " + key  + " >>> value = " + value);  
 57                     }  
 58                    }  
 59             } catch (Exception e) {  
 60                 log.debug(">>>>>>>> PropertiesUtil取得全部的key和value出错  :  " + e);  
 61                 e.printStackTrace();  
 62             }  
 63         }  
 64           
 65           
 66         return map ;  
 67     }  
 68       
 69       
 70       
 71     /** 
 72      * @desc 根据key得到properties的value 
 73      * @param propertiesKey 
 74      * @return Map 
 75      * @author:  lic 
 76      * @datetime:2011-12-14 上午10:02:13 
 77      */  
 78     @SuppressWarnings("rawtypes")  
 79     public static String getPropertiesValue( String propertieskey , String filename){  
 80               
 81             String value = "" ;  
 82               
 83             if ( propertieskey != null && !propertieskey.equals("") ){  
 84                 String pkey = filename;  
 85                 String  url = PropertiesUtil.getPropertiesPath(filename);  
 86                 try {  
 87                     InputStream in =new BufferedInputStream(new FileInputStream(url));  
 88 //                  this.properties.load(in);  
 89                     PropertiesUtil.properties.load(in);  
 90                     System.out.println("1");  
 91 //                    Enumeration en = properties.propertyNames();  
 92                       Enumeration en = PropertiesUtil.properties.propertyNames();  
 93                        while (en.hasMoreElements()) {  
 94                            String key = (String) en.nextElement();  
 95                            if ( key.equals(propertieskey) ){  
 96 //                             value = this.properties.getProperty(key);  
 97                                value = PropertiesUtil.properties.getProperty(key);  
 98                            }  
 99                        }  
100                 } catch (Exception e) {  
101                     e.printStackTrace();  
102                 }  
103             }  
104               
105               
106             return value ;  
107         }  
108           
109       
110   
111     /** 
112      * @desc 根据properties文件名获取路径 
113      * @param propertiesKey 
114      * @return Map 
115      * @author:  lic 
116      * @datetime:2011-12-13 下午15:53:58 
117      */  
118     public static String getPropertiesPath( String propertiesName ){  
119               
120             String path = "";  
121             if ( propertiesName != null && !propertiesName.equals("") ){  
122                 path = PropertiesUtil.class.getClassLoader().getResource(propertiesName).toString().substring(6);  
123             }  
124               
125             return path ;  
126     }  
127           
128           
129 }  

 

java读取properties

标签:logging   source   ati   while   nbsp   ble   ash   port   获取路径   

原文地址:http://www.cnblogs.com/sunny-sl/p/7262669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!