java读取properties

Posted Sunny*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java读取properties相关的知识,希望对你有一定的参考价值。

  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的主要内容,如果未能解决你的问题,请参考以下文章

java 读取properties配置文件代码如下

Java 代码读取properties文件

java 怎么读取resource properties 并且解析成map

java读取properties配置文件路径问题

Java读取properties配置文件

java读取资源文件(Properties)