JAva propertiesプロパティファイル値の読み出し
2800 ワード
package com.zte.cbm.bis.business.common;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import com.zte.ssb.framework.SSBBus;
import com.zte.ssb.framework.common.log.Log;
/**
*
*
* @author
*
* @Time
*/
public class ConfigurationRead {
/**
* , PFILE
*/
private final static String PFILE = "linkOA.properties";
/**
*
*/
private URI uri = null;
/**
*
*/
private long m_lastModifiedTime = 0;
/**
*
*/
private File m_file = null;
/**
*
*/
private Properties m_props = null;
/**
*
*/
private static ConfigurationRead m_instance = new ConfigurationRead();
Log log = SSBBus.getLog(getClass());
/**
*
*
* @throws URISyntaxException
*/
private ConfigurationRead() {
try {
m_lastModifiedTime = getFile().lastModified();
if (m_lastModifiedTime == 0) {
System.err.println(PFILE + "file does not exist!");
}
m_props = new Properties();
m_props.load(new FileInputStream(getFile()));
} catch (URISyntaxException e) {
System.err.println(PFILE + " ");
log.debug(e.getMessage(),e);
} catch (Exception e) {
System.err.println(PFILE + " ");
log.debug(e.getMessage(),e);
}
}
/**
* ClassPath
*
* @return File
* @throws URISyntaxException
*/
private File getFile() throws URISyntaxException {
URI fileUri = this.getClass().getClassLoader().getResource(PFILE)
.toURI();
m_file = new File(fileUri);
return m_file;
}
/**
*
*
* @return ConfigurationRead
*/
public synchronized static ConfigurationRead getInstance() {
return m_instance;
}
/**
*
*/
public String getConfigItem(String name, String defaultVal) {
long newTime = m_file.lastModified();
//
if (newTime == 0) {
//
if (m_lastModifiedTime == 0) {
System.err.println(PFILE + " file does not exist!");
} else {
System.err.println(PFILE + " file was deleted!!");
}
return defaultVal;
} else if (newTime > m_lastModifiedTime) {
m_props.clear();
try {
m_props.load(new FileInputStream(getFile()));
} catch (Exception e) {
System.err.println(" ");
log.debug(e.getMessage(),e);
}
}
m_lastModifiedTime = newTime;
String val = m_props.getProperty(name);
if (val == null) {
return defaultVal;
} else {
return val;
}
}
/**
*
*
* @param name
*
* @return ( ), ( )
*/
public String getConfigItem(String name) {
return getConfigItem(name, "");
}
}