ITeye博客

UTIL工具类

Dragonmandance · 2017-02-08 17:59 · java · 工具类 · 编程语言 · Markdown

properties文件读取


public class PropertiesUtil {
	private static final LoggerAdapter logger = LoggerAdapterFactory
			.getLogger(MTPLogType.WEB);
	/**
	 * 读取properties文件,返回Map
	 * @param resourceFileName
	 * @return
	 */
	public static Map<String, String> loadFile(String resourceFileName) {
		Map<String, String> data = new HashMap<String, String>();
		InputStream is = null;
		try {
			is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(resourceFileName);

			Properties properties = new Properties();
			properties.load(is);
			for (Map.Entry<Object, Object> entry : properties.entrySet()) {
				data.put((String) entry.getKey(), (String) entry.getValue());
			}
			return data;
		} catch (Exception e) {
			logger.error("load "+resourceFileName+" error", e);
			return null;
		} finally {
			if (is != null) {
				try {
					is.close();
					is = null;
				} catch (IOException e) {
					logger.error("", e);
				}
			}
		}
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。