package net.onerock.topic.utils;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 根据key 读取value
*/
public class PropertiesUtil {
private static PropertiesUtil propertiesUtil = null;
private static Properties properties = null;
private PropertiesUtil() {
super();
}
public static PropertiesUtil getInstance() {
if (propertiesUtil == null) {
synchronized (PropertiesUtil.class) {
propertiesUtil = new PropertiesUtil();
}
}
return propertiesUtil;
}
public static String getPropertiesByKey(String key) {
if (properties == null) {
ClassPathResource cr = new ClassPathResource("conf/config.properties");
try {
InputStream inputStream = cr.getInputStream();
if (inputStream != null) {
properties = new Properties();
properties.load(cr.getInputStream());
}
} catch (IOException e) {
//log.error("load config-com.properties fail !", e);
}
}
if (properties == null)
return null;
return properties.getProperty(key);
}
public static void main(String[] args) {
String value = PropertiesUtil.getPropertiesByKey("ofdpath");
System.out.println(value);
}
}