讀取修改配置文件

讀取修改配置文件
分類: util類 2014-07-15 14:37 37人閱讀 評論(0) 收藏 舉報
ResourceBundle
PropertiesUtil.class.getClassLoader().getResource
PropertiesLoaderUtils.loadProperties(new ClassPathResource(propertyName));

[java] view plaincopy
package com.ffcs.aaa.utils;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;


public class PropertiesUtil {
static Logger log = Logger.getLogger("smsTaskLog");
/**
* 獲取指定配置文件中所以的數據
*
* @param propertyName
* 調用方式: 1.配置文件放在resource源包下,不用加後綴
* PropertiesUtil.loadWarningMessage("warn_message"); 2.放在包裏面的
* PropertiesUtil.loadWarningMessage("com.test.message");
* @return
*/
public static Map<String, String> loadProperties(String propertyName) {
// 獲得資源包
ResourceBundle rb=null;
try {rb = ResourceBundle.getBundle(propertyName.trim());
} catch (Exception e) {log.error(e.getMessage()); }

// 通過資源包拿到所有的key
Enumeration<String> allKey = rb.getKeys();
// 遍歷key 得到 value
Map<String, String> messageMap=new HashMap<String, String>();
while (allKey.hasMoreElements()) {
String key = allKey.nextElement();
String value = (String) rb.getString(key);
messageMap.put(key, value);
}
return messageMap;
}

/**
* 傳遞鍵值對的Map,更新properties文件
*
* @param fileName
* 文件名(放在resource源包目錄下),需要後綴 warn_message.properties
* @param keyValueMap
* 鍵值對Map
*/
public static void updateProperties(String fileName,Map<String, String> keyValueMap) {
//getResource方法使用了utf-8對路徑信息進行了編碼,當路徑中存在中文和空格時,他會對這些字符進行轉換,這樣,
//得到的往往不是我們想要的真實路徑,在此,調用了URLDecoder的decode方法進行解碼,以便得到原始的中文及空格路徑。
String filePath = PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();
Properties props = null;
BufferedWriter bw = null;

try {
filePath = URLDecoder.decode(filePath,"utf-8");
log.debug("updateProperties propertiesPath:" + filePath);
props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
// log.debug("updateProperties old:"+props);

// 寫入屬性文件
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));

props.clear();// 清空舊的文件

for (String key : keyValueMap.keySet())
props.setProperty(key, keyValueMap.get(key));

log.debug("updateProperties new:"+props);
props.store(bw, "");
} catch (IOException e) {
log.error(e.getMessage());
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 判斷指定配置文件中是否存在該項 Spring 提供的 PropertiesLoaderUtils 允許您直接通過基於類路徑的文件地址加載屬性資源
* 最大的好處就是:實時加載配置文件,修改後立即生效,不必重啓
*
* @param propertyName
* 要加上後綴:warn_message.properties
* @param key
* @return true:false
*/
public static boolean containKey(String propertyName, String key) {
Properties props = null;
try {
props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(propertyName));
} catch (IOException e) {
log.error(e.getMessage());
}
log.debug("props content:"+props);
return props.containsKey(key);
}



private void test() {
List<String> l = new ArrayList<String>();
l.add("192.168.14.63");l.add("192.168.14.62");l.add("192.168.14.61");

Map<String, String> map = new HashMap<String, String>();
for (String key : l) {
boolean containKey = PropertiesUtil.containKey("warn_message.properties", key);
log.debug(key+":"+containKey);
map.put(key, "");
}
PropertiesUtil.updateProperties("message.properties", map);
System.out.println("================================================");
for (String key : l) {
boolean containKey = PropertiesUtil.containKey("warn_message.properties", key);
log.debug(key+":"+containKey);
map.put(key, "");
}
}


public static void main(String[] args) {
loadProperties("a");
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章