
com.aspire.nm.component.commonUtil.constants.ConstantConfig Maven / Gradle / Ivy
package com.aspire.nm.component.commonUtil.constants;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.aspire.nm.component.commonUtil.file.FileUtil;
public class ConstantConfig {
private static Map propertiesMap = new HashMap();
private static Map contentMap = new HashMap();
/**
* 获取classPath下 properties
* @param path classPath下资源文件的相对路径
* @return
*/
public static Properties getProperties(String path){
if(propertiesMap.get(path) == null){
Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
properties.load(new InputStreamReader(loader.getResourceAsStream(path), "UTF-8"));
} catch (IOException e) {
return null;
}
propertiesMap.put(path, properties);
}
return propertiesMap.get(path);
}
/**
* 获取classPath下 properties资源文件的value
* @param path classPath下资源文件的相对路径
* @param key 资源文件的key
* @return
*/
public static String getPropertiesValue(String path,String key){
if(propertiesMap.get(path) == null){
Properties properties = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try {
properties.load(new InputStreamReader(loader.getResourceAsStream(path), "UTF-8"));
} catch (IOException e) {
return null;
}
propertiesMap.put(path, properties);
}
return propertiesMap.get(path).getProperty(key);
}
/**
* 获取classPath下 资源文件的内容
* @param path classPath下资源文件的相对路径
* @return
*/
public static String getContentValue(String path){
if(contentMap.get(path) == null){
try {
contentMap.put(path, FileUtil.getContentFromClassPath(path));
} catch (IOException e) {
return null;
}
}
return contentMap.get(path);
}
/**
* 获取classPath下 资源文件的内容,无缓存
* @param path classPath下资源文件的相对路径
* @return
*/
public static String getContentValueUnCache(String path){
try {
return FileUtil.getContentFromClassPath(path);
} catch (IOException e) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy