package com.trisun.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

/**
 * Properties Util函数.
 * @author tgl
 */
public class PropertiesUtils {
	private static final String DEFAULT_ENCODING = "UTF-8";
	private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);
	private static PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();
	/**
	 * 载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的载入.
	 * 文件路径使用Spring Resource格式, 文件编码使用UTF-8.
	 * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
	 */
	public static Properties loadProperties(String... resourcesPaths) throws IOException {
		Properties props = new Properties();
		for (String location : resourcesPaths) {
			logger.debug("Loading properties file from:" + location);
			InputStream is = null;
			try {
				Resource resource = resourceLoader.getResource(location);
				is = resource.getInputStream();
				propertiesPersister.load(props, new InputStreamReader(is, DEFAULT_ENCODING));
			} catch (IOException ex) {
				logger.info("Could not load properties from classpath:" + location + ": " + ex.getMessage());
			} finally {
				if (is != null) {
					is.close();
				}
			}
		}
		return props;
	}
}
最近下载更多
images  LV1 2019年2月11日
1054403833  LV12 2018年1月19日
tangxiao  LV1 2017年11月23日
codesheng  LV2 2017年11月8日
tanking  LV1 2017年9月18日
hillchen  LV1 2017年8月15日
雪狐ypc  LV9 2017年8月2日
window24  LV2 2017年6月17日
test222  LV1 2017年4月11日
一万年太久  LV1 2017年3月30日
最近浏览更多
tangjun  LV17 2022年8月31日
fengzhou888  LV5 2021年12月6日
zhyp29  LV3 2020年12月18日
gan857569302  LV9 2020年6月28日
hfk2020  LV2 2020年5月22日
一页尘埃  LV7 2020年4月30日
Rommel  LV27 2020年4月18日
带刀的刺猬  LV9 2019年9月20日
最代码安逸  LV15 2019年9月5日
images  LV1 2019年2月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友