比如创建一个类如下:
import java.util.Properties;
import org.apache.log4j.Logger;
public class SMPConfigReader {
private static final Logger log = Logger.getLogger(SMPConfigReader.class);
private static final String FILENAME = "smp.properties";
private static SMPConfigReader loader = null;
private Properties m_props = null;
private String vpasCodebase;
private String vpasVersion;
private String screenshotparameterconfigurepath;
public String getScreenshotparameterconfigurepath() {
return screenshotparameterconfigurepath;
}
public void setScreenshotparameterconfigurepath(String screenshotparameterconfigurepath) {
this.screenshotparameterconfigurepath = screenshotparameterconfigurepath;
}
public String getPlayerpath() {
return playerpath;
}
public void setPlayerpath(String playerpath) {
this.playerpath = playerpath;
}
private SMPConfigReader() {
this.m_props = new Properties();
try {
this.m_props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(FILENAME));
this.vpasCodebase = this.m_props.getProperty("vpas.codebase", "CCDP3.0_VPAS.cab").trim();
this.vpasVersion = this.m_props.getProperty("vpas.version", "1,0,0,1").trim();
this.screenshotparameterconfigurepath=this.m_props.getProperty("screenshotparameterconfigurepath", "/webapps/iptvsmp/general/active/VPAS_CFG.txt").trim();
} catch (Exception e) {
log.error("SMPConfigReader", e);
}
}
/**
* Get configuration loader instance.
*
* @return
*/
public static SMPConfigReader getInstance() {
if (null == loader) {
loader = createInstance();
}
return loader;
}
private static synchronized SMPConfigReader createInstance() {
if (null != loader) {
return loader;
}
return new SMPConfigReader();
}
public String getVpasCodebase() {
return vpasCodebase;
}
public void setVpasCodebase(String vpasCodebase) {
this.vpasCodebase = vpasCodebase;
}
public String getVpasVersion() {
return vpasVersion;
}
public void setVpasVersion(String vpasVersion) {
this.vpasVersion = vpasVersion;
}
}
这样当我们要读取某个参数时,比如读取screenshotparameterconfigurepath参数时,可以通过下面这种方式读取:
SMPConfigReader.getInstance().getScreenshotparameterconfigurepath()