com.dexcoder.commons.utils.PropertyUtils Maven / Gradle / Ivy
package com.dexcoder.commons.utils;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import com.dexcoder.commons.exceptions.CommonsAssistantException;
/**
* 属性文件操作辅助类
*
* User: liyd
* Date: 14-1-7
* Time: 上午11:24
*/
public final class PropertyUtils {
/**
* 属性文件后缀
*/
private static final String PRO_SUFFIX = ".properties";
/**
* 配置文件保存map
*/
private static Map propMap = new HashMap();
/**
* 加载资源文件
*
* @param resourceName
* @return
*/
public static InputStream loadResource(String resourceName) {
try {
File configFile = getConfigFile(resourceName);
if (configFile == null) {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
return is;
} else {
return new FileInputStream(configFile);
}
} catch (FileNotFoundException e) {
throw new CommonsAssistantException("加载xml文件失败:" + resourceName, e);
}
}
/**
* 加载properties文件
*
* @param resourceName the resource name
*/
public static void loadProperties(String resourceName) {
try {
if (!StrUtils.endsWith(resourceName, PRO_SUFFIX)) {
resourceName += PRO_SUFFIX;
}
Properties prop = new Properties();
prop.load(loadResource(resourceName));
Iterator> iterator = prop.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy