读取修改配置文件

ResourceBundle
PropertiesUtil.class.getClassLoader().getResource
PropertiesLoaderUtils.loadProperties(new ClassPathResource(propertyName));


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");
	}

}


你可能感兴趣的:(读取修改配置文件)