posts - 33,  comments - 17,  trackbacks - 0
           1// 從資源文件里讀取值的類,文件后綴不一定要.Properties,只要里面內容如:url=www.cnsec.net 
           2可通過key(url)取得值-www.cnsec.net,簡單、強大 
           3import java.io.File;
           4import java.io.FileInputStream;
           5import java.io.FileNotFoundException;
           6import java.io.IOException;
           7import java.util.Properties;
           8/**
           9 * ReadProperties.java
          10 * Description: 讀取操作屬性配置文件
          11 * @author li.b
          12 * @version 2.0
          13 * Jun 26, 2008
          14 */

          15public class ReadProperties {
          16
          17    /**
          18     * Description: 獲取屬性配置文件
          19     * @param path 資源文件路徑
          20     * @return    Properties Object
          21     * @throws FileNotFoundException
          22     * @throws IOException
          23     */

          24    public static Properties getProperties(String path) throws FileNotFoundException, IOException{
          25        Properties props = null;
          26        File file = new File(path);
          27        if(file.exists() && file.isFile()){
          28            props = new Properties();
          29            props.load(new FileInputStream(file));
          30        }
          else{
          31            System.out.println(file.toString() + "不存在!");
          32        }

          33        return props;
          34    }

          35    
          36    /**
          37     * Description: 從屬性文件獲取值
          38     * @param props Properties Object
          39     * @param key
          40     * @return 通過key匹配到的value
          41     */

          42    public static String getValue(Properties props,String key,String encod){
          43        String result = "";
          44        String en = "";
          45        String localEN = System.getProperty("file.encoding");
          46        if(encod !=null && !encod.equals("") ){
          47            en = encod;
          48        }
          else{
          49            en = localEN;
          50        }

          51        try {
          52            key = new String(key.getBytes(en),"ISO-8859-1");
          53            result = props.getProperty(key);
          54            if(!result.equals("")){
          55                    result = new String(result.getBytes("ISO-8859-1"),en);                
          56            }

          57        }
           catch (Exception e) {
          58        }
          finally{
          59            if(result == null)result = "";
          60            return result;
          61        }

          62    }

          63    
          64    public static String getValue(Properties props,String key){
          65        return getValue(props, key, "");
          66    }

          67
          68}
          posted on 2008-07-23 17:43 scea2009 閱讀(414) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          PL/SQL存儲過程與函數

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 屯门区| 彰化市| 准格尔旗| 泰来县| 安新县| 梅州市| 和顺县| 木兰县| 义乌市| 乐陵市| 鹿邑县| 宁海县| 平邑县| 望城县| 广昌县| 昌图县| 延长县| 神农架林区| 西宁市| 饶平县| 法库县| 墨玉县| 延津县| 平谷区| 醴陵市| 衡水市| 新乡市| 永昌县| 高唐县| 理塘县| 饶阳县| 建宁县| 常德市| 乐至县| 平定县| 故城县| 花莲县| 子长县| 汶川县| 滨州市| 明溪县|