Java操作屬性檔案之工具類

來源:互聯網
上載者:User

標籤:

  最近空閑時間整理一下平時常用的一下工具類,重複造輪子實在是浪費時間,如果不正確或者有待改善的地方,歡迎指教...

package com.hsuchan.business.utils;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Properties;import org.apache.log4j.Logger;/** * Java屬性檔案操作工具類 * <p>由於每個人的需求不一樣,所以本工具只提供常用的功能(包括根據鍵名擷取索引值,根據鍵名修改索引值), * 如果不滿足,自個完全可以修改,在本工具類中,我們假設屬性檔案已經存在,並且檔案中已經存在我們要操作的key</p> * @author HsuChan * @version v1.0.0 2015-06-19 */public class PropertiesUtil {    private static Properties properties= new Properties();    private static Logger logger = Logger.getLogger(PropertiesUtil.class);    /**測試用 屬性檔案名稱*/    private static final String PROPERTIES_FILE_PATH="/config.properties";    /**測試用 操作的鍵名*/    private static final String KEY_NAME="content";            /**     * 初始化properties     * @param path 屬性檔案路徑,如:/config.properties     */    private static void initProperties(String path) {                try {            InputStream ipsm = PropertiesUtil.class.getResourceAsStream(path);            properties.load(ipsm);            ipsm.close();        } catch (IOException e) {            logger.error(e.getMessage());        }    }    /**     * 根據鍵名key擷取屬性檔案中對應的索引值     * @param path 屬性檔案路徑,如:/config.properties     * @param key 屬性檔案 鍵名     * @return     */    public static String getValueByKey(String path,String key){        //如果properties為空白,則初始化        if(properties.isEmpty())            initProperties(path);        return properties.getProperty(key);    }        /**     * 通過鍵名key修改其對應的值為value     * @param path 屬性檔案路徑,如:/config.properties     * @param key  屬性檔案 鍵名     * @param value 屬性檔案 索引值     */    public static void modifyValueByKey(String path,String key,String value) {        //如果properties為空白,則初始化        if(properties.isEmpty())            initProperties(path);        //修改值        properties.setProperty(key, value);               try {            //擷取檔案路徑            URL url = PropertiesUtil.class.getResource(path);            FileOutputStream fos = new FileOutputStream(new File(url.toURI()));            //儲存到檔案            properties.store(fos, "modify the value of key");            fos.close();        } catch (Exception e) {            logger.error(e.getMessage());        }    }          /**     * 測試     * @param args     */    public static void main(String[] args) {        //取值樣本        System.out.println("修改前:"+getValueByKey(PROPERTIES_FILE_PATH,KEY_NAME));        //修改樣本        modifyValueByKey(PROPERTIES_FILE_PATH,KEY_NAME,"3333311s");        //取值樣本        System.out.println("修改後:"+getValueByKey(PROPERTIES_FILE_PATH,KEY_NAME));            }}

轉載請註明出處,謝謝。

Java操作屬性檔案之工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.