<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    yxhxj2006

    常用鏈接

    統計

    最新評論

    JAVA讀取Properties配置文件


    JAVA獲取文件絕對路徑Thread.currentThread().getContextClassLoader().getResource("abc.properties");                                   


     配置文件:XXX.properties 常用操作


     1:加載properties配置文件
      /**
         * 根據key從Properties對象中得到相應的值
         * 
    @param key
         * 
    @return 對應的值
         
    */
        public static String getValue(String key) {
            if (p !=  null) {
                return p.getProperty(key);
            }
            String property = null
            InputStream in = null;
            
            try {
                System.out.println("gleepay.properties is path : " + gleepayURL.getFile());
                File file = new File(gleepayURL.getFile());
                in = new FileInputStream(file);
                p = new Properties();
                p.load(in);
                property = p.getProperty(key);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }  finally {
                if (in != null) {
                    try {
                            in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return property;
        }

    2:寫入propertis配置信息
    /**
         * 寫入properties信息
         * 
    @param parameterKey
         * 
    @param parameterValue
         * 
         * 
    @return
         
    */
        public static void writeProperties_MACkey(String parameterKey, String parameterValue) {
            String filePath = MACkeyURL.getFile();
            Properties prop = new Properties();
            try {
                InputStream fis = new FileInputStream(filePath);
                // 從輸入流中讀取屬性列表(鍵和元素對)
                prop.load(fis);
                // 調用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
                
    // 強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
                OutputStream fos = new FileOutputStream(filePath);
                prop.setProperty(parameterKey, parameterValue);
                // 以適合使用 load 方法加載到 Properties 表中的格式,
                
    // 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
                prop.store(fos, "Update '" + parameterKey + "' value");
            } catch (IOException e) {
                System.err.println("Visit " + filePath + " for updating "
                        + parameterKey + " value error");
                e.printStackTrace();
            }
        }
        

    3:根據key讀取propertis文件value值
        /**
         * 根據key讀取value
         * 
    @param filePath
         * 
    @param key
         * 
    @return 對應的值
         
    */
        public static String readValueByKey(String filePath, String key) {
            System.out.println("properties file is path : " + filePath);
            Properties props = new Properties();
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(filePath));
                props.load(in);
                String value = props.getProperty(key);
                System.out.println(" --> " + key + "=" + value);
                return value;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        
    4:讀取properties配置文件所有信息
        /**
         *  讀取properties的全部信息
         * 
    @param filePath
         * 
    @return
         
    */
        @SuppressWarnings("unchecked")
        public static void readPropertiesAll(String filePath) {
            System.out.println("properties file is path : " + filePath);
            Properties props = new Properties();
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(filePath));
                props.load(in);
                Enumeration en = props.propertyNames();
                while (en.hasMoreElements()) {
                    String key = (String) en.nextElement();
                    String Property = props.getProperty(key);
                    System.out.println(" --> " + key + "=" + Property);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    posted on 2013-08-08 23:43 奮斗成就男人 閱讀(167) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲AV无码专区国产乱码4SE| 亚洲综合色区中文字幕| 99久久精品免费视频| 亚洲av无码不卡久久| 亚洲精品一级无码鲁丝片| 日韩免费人妻AV无码专区蜜桃| 亚洲欧洲精品成人久久曰| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲AV无码一区二区三区牛牛| 亚洲成a人片在线观看国产| 久久久久久一品道精品免费看| 国内精品久久久久影院亚洲| 亚洲天堂中文字幕在线| 国产精品1024永久免费视频| 青青草国产免费国产是公开| 亚洲高清在线mv| 国产亚洲精品无码专区| 无码日韩人妻av一区免费| 成人片黄网站色大片免费观看cn| 亚洲一区在线视频| 中文字幕亚洲一区二区va在线| 成人特黄a级毛片免费视频| 成人久久免费网站| 羞羞视频免费网站入口| 亚洲伊人久久大香线蕉在观| 国产亚洲美女精品久久久| 尤物永久免费AV无码网站| 日日麻批免费40分钟无码| 一级视频免费观看| 亚洲乱人伦中文字幕无码| 亚洲日韩中文字幕| 亚洲国产精品第一区二区| 亚洲成?v人片天堂网无码| 成年私人影院免费视频网站| 91精品免费观看| 国产无遮挡裸体免费视频在线观看| 狼人大香伊蕉国产WWW亚洲| 亚洲宅男精品一区在线观看| 亚洲高清在线观看| 中文字幕精品无码亚洲字 | 亚洲综合激情六月婷婷在线观看 |