Properties配置文件读取工具类

Properties配置文件读取工具类

import java.io.*;
import java.util.*;

/**
 * @author Cool.R 2020-12-03
 * 增加多个配置文件支持
 */
public class ResourceUtils {
	private static final String defaultResourceFile = "application";
	private static Map<String, ResourceBundle> bundles = new HashMap<String, ResourceBundle>();
	private static Map<String,Map<String,String>> resource = new HashMap<String, Map<String,String>>();
	private static void loadResourceBundle(String resourceFile) {
		ResourceBundle bundle = ResourceBundle.getBundle(resourceFile);
		bundles.put(resourceFile, bundle);
	}

	/**
	 * 获取配置文件参数
	 * @param name
	 * @return
	 */
	public static final String getConfigByName(String name) {
		return getConfigByName(name,defaultResourceFile);
	}

	public static final String getConfigByName(String name, String resourceFile) {
		if (bundles.get(resourceFile) == null) {
				loadResourceBundle(resourceFile);
		}
		ResourceBundle bundle  = bundles.get(resourceFile);
		
		//存在则翻译
		if(bundle.containsKey(name)){
			try {
				return new String(bundle.getString(name).getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		return name;//如果未找到,则返回原始值
	}

	public static Map<String,String> getAll(String resourceFile) throws Exception{
		if (bundles.get(resourceFile) == null) {
			loadResourceBundle(resourceFile);
			
		}
		if(resource.get(resourceFile) ==null){
			loadResource(resourceFile);
		}
		return resource.get(resourceFile);
		
		//return result;
	}
	
	public static void loadResource(String resourceFile) throws Exception{
		Map<String,String> result = new LinkedHashMap<String, String>();
		Properties properties = new Properties();
		InputStream inputStream = ResourceUtils.class.getClassLoader().getResourceAsStream(resourceFile+".properties");
		try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            properties.load(bf);
            Set<Object> keys  = properties.keySet();
    		for(Object key:keys){
    			result.put(key.toString(), properties.get(key).toString());
    			//result.put(key.toString(), new String(properties.get(key).toString().getBytes("ISO-8859-1"),"UTF-8"));
    		}
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
        	inputStream.close(); // 关闭流
        }
		
		resource.put(resourceFile, result);
	}
	
	/**
	 * properties文件根据key更新value
	 * @param key
	 * @param value
	 * @param path 路径  
	 * @throws Exception
	 */
	public static void putValueByKey(String key,String value,String path)throws Exception{
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			Properties props = new Properties();
			System.out.println(path);
			inputStream = new FileInputStream(path);
			props.load(inputStream);
			props.setProperty(key, value);
			outputStream = new FileOutputStream(path);
			props.store(outputStream, "Update " + key );
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			inputStream.close();
			outputStream.close();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值