java讀取properties

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.