作者:会说话de狗尾草 | 来源:互联网 | 2023-10-12 16:39
import?java.io.IOException;import?java.io.InputStream;import?java.util.Properties;impor
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
* PropertiesUtil
*
* @author yangshenhui
*/
public class PropertiesUtil {
private static Logger logger = Logger.getLogger(PropertiesUtil.class);
private PropertiesUtil() {
}
private static class SingletonHolder {
private final static PropertiesUtil INSTANCE = new PropertiesUtil();
}
public static PropertiesUtil getInstance() {
return SingletonHolder.INSTANCE;
}
public Properties getProperties(String fileName) {
Properties Properties = new Properties();
InputStream inputStream = null;
try {
inputStream = PropertiesUtil.class.getClassLoader()
.getResourceAsStream(fileName);
Properties.load(inputStream);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return Properties;
}
}
Java学习资料-Java最优单例模式