對Java配置文件Properties的讀取、寫入與更新操作


http://breezylee.iteye.com/blog/1340868

 

 

對Java配置文件Properties的讀取、寫入與更新操作
注:當前項目路徑是
String filepath=System.getProperty("user.dir");

對下面的程序很有用...

/**
* 實現對Java配置文件Properties的讀取、寫入與更新操作
*/
package test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;


/**
* @author
* @version
*/
public class SetSystemProperty {
    //屬性文件的路徑
    static String profilepath="mail.properties";
    /**
    * 采用靜態方法
    */
    private static Properties props = new Properties();
    static {
        try {
            props.load(new FileInputStream(profilepath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.exit(-1);
        } catch (IOException e) {       
            System.exit(-1);
        }
    }

    /**
    * 讀取屬性文件中相應鍵的值
    * @param key
    *            主鍵
    * @return String
    */
    public static String getKeyValue(String key) {
        return props.getProperty(key);
    }

    /**
    * 根據主鍵key讀取主鍵的值value
    * @param filePath 屬性文件路徑
    * @param key 鍵名
    */
    public static String readValue(String filePath, String key) {
        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;
        }
    }
   
    /**
    * 更新(或插入)一對properties信息(主鍵及其鍵值)
    * 如果該主鍵已經存在,更新該主鍵的值;
    * 如果該主鍵不存在,則插件一對鍵值。
    * @param keyname 鍵名
    * @param keyvalue 鍵值
    */
    public static void writeProperties(String keyname,String keyvalue) {       
        try {
            // 調用 Hashtable 的方法 put,使用 getProperty 方法提供並行性。
            // 強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
            OutputStream fos = new FileOutputStream(profilepath);
            props.setProperty(keyname, keyvalue);
            // 以適合使用 load 方法加載到 Properties 表中的格式,
            // 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
            props.store(fos, "Update '" + keyname + "' value");
        } catch (IOException e) {
            System.err.println("屬性文件更新錯誤");
        }
    }

    /**
    * 更新properties文件的鍵值對
    * 如果該主鍵已經存在,更新該主鍵的值;
    * 如果該主鍵不存在,則插件一對鍵值。
    * @param keyname 鍵名
    * @param keyvalue 鍵值
    */
    public void updateProperties(String keyname,String keyvalue) {
        try {
            props.load(new FileInputStream(profilepath));
            // 調用 Hashtable 的方法 put,使用 getProperty 方法提供並行性。
            // 強制要求為屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。
            OutputStream fos = new FileOutputStream(profilepath);           
            props.setProperty(keyname, keyvalue);
            // 以適合使用 load 方法加載到 Properties 表中的格式,
            // 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
            props.store(fos, "Update '" + keyname + "' value");
        } catch (IOException e) {
            System.err.println("屬性文件更新錯誤");
        }
    }
    //測試代碼
    public static void main(String[] args) {
        readValue("mail.properties", "MAIL_SERVER_PASSWORD");
        writeProperties("MAIL_SERVER_INCOMING", "327@qq.com");       
        System.out.println("操作完成");
    }
}

 

另一個例子:此處的文件common.properties和 Config類都在class src下面

Java代碼   收藏代碼
  1. public static String getValue(String key) {  
  2.         try {  
  3.             InputStream inputStream = Config.class.getClassLoader().getResourceAsStream("common.properties");  
  4.             property.load(inputStream);  
  5.         } catch (Exception e) {  
  6.             e.printStackTrace();  
  7.         }  
  8.         return (String) property.get(key);  
  9.     }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM