工具类
package com.chanct.idap.maintenance.device.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*
*私有静态内部类实现线程安全的单例
* @author Canada
*
*/
public class GetValueFromProperties {
private final String propertiesPath="/config/properties/deviceInfo.properties";
private Properties pros = null;
private static volatile GetValueFromProperties instance;
public static GetValueFromProperties getInstance() {
if (instance == null) {
synchronized(GetValueFromProperties.class) {
if(instance == null) {
instance = new GetValueFromProperties();
}
}
}
return instance;
}
public String getValue(String key){
return pros.getProperty(key);
}
private GetValueFromProperties(){
readConfig();
}
private void readConfig() {
pros = new Properties();
InputStream in = null;
try {
in = GetValueFromProperties.class.getResourceAsStream(propertiesPath);
pros.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
调用1
String outPath = GetValuePathsFromProperties.getInstance().getValue("outXmlPath");