读取properties配置文件的工具类
来源:互联网 发布:婚礼致辞 知乎 编辑:程序博客网 时间:2024/06/06 13:04
private static final Logger LOGGER = Logger.getLogger(PropertiesUtil.class);
private final Properties props;
public PropertiesUtil(final Properties props) {
this.props = props;
}
public PropertiesUtil(final String propertiesFileName) {
final Properties properties = new Properties();
InputStreamReader in = null;
try {
in = new InputStreamReader(new FileInputStream(this.getClass().getResource("/").getPath()+propertiesFileName), "UTF-8");
//in = PropertiesUtil.class.getClassLoader().getResourceAsStream(propertiesFileName);
properties.load(in);
} catch (final IOException ioe) {
LOGGER.error("Unable to read " + propertiesFileName, ioe);
} finally {
if (in != null) {
try {
in.close();
} catch (final IOException ioe) {
LOGGER.error("Unable to close " + propertiesFileName, ioe);
}
}
}
this.props = properties;
}
public String getStringProperty(final String name) {
return props.getProperty(name);
}
public int getIntegerProperty(final String name, final int defaultValue) {
String prop = props.getProperty(name);
if (prop != null) {
try {
return Integer.parseInt(prop);
} catch (final Exception ignored) {
return defaultValue;
}
}
return defaultValue;
}
public long getLongProperty(final String name, final long defaultValue) {
String prop = props.getProperty(name);
if (prop != null) {
try {
return Long.parseLong(prop);
} catch (final Exception ignored) {
return defaultValue;
}
}
return defaultValue;
}
public float getFloatProperty(final String name,final float defaultValue)
{
String prop = props.getProperty(name);
if (prop != null) {
try {
return Float.parseFloat(prop);
} catch (final Exception ignored) {
return defaultValue;
}
}
return defaultValue;
}
public String getStringProperty(final String name, final String defaultValue) {
final String prop = getStringProperty(name);
return (prop == null) ? defaultValue : prop;
}
public boolean getBooleanProperty(final String name) {
return getBooleanProperty(name, false);
}
public boolean getBooleanProperty(final String name, final boolean defaultValue) {
final String prop = getStringProperty(name);
return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
}
public Set<Object> keySet()
{
return props.keySet();
}
public Collection<Object> values()
{
return props.values();
}
/*
*private final static Logger LOGGER = Logger.getLogger(PropertiesUtil.class);
private final Properties prop;
private final String fileName;
public PropertiesUtil(String fileName) {
prop = new Properties();
this.fileName = fileName;
InputStreamReader is = null;
try {
is = new InputStreamReader(new FileInputStream(this.getClass().getResource("/").getPath()+fileName), "UTF-8");
prop.load(is);
} catch (FileNotFoundException e) {
LOGGER.error("", e);
} catch (Exception e) {
LOGGER.error("", e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
LOGGER.error("", e);
}
}
}
}
public String getValue(String key) {
String value = prop.getProperty(key);
if (value == null) {
throw new IllegalArgumentException("Key: " + key + " IS NOT EXIST IN " + fileName);
}
return value;
}
*/
}
在其他类中调用的方式
private static PropertiesUtil PropertiesUtil =
new PropertiesUtil("sectionDoubleBase.properties");
//参数为相应的key
public static String getSectionDoubleBase(String type){
String SectionDoubleBase=null;
if(null==SectionDoubleBase){
SectionDoubleBase= PropertiesUtil.getStringProperty(type);
}
return SectionDoubleBase;
}
// 中文字符转换为UTF-8
private static String convertUTF8(String prop)
{
if(!StringUtils.hasText(prop))
{
return null;
}
try {
prop=new String(prop.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return prop;
}
//获取所有的值
public static List<String> getAllSectionDoubleBase(){
//获取key的个数
Set set = PropertiesUtil.keySet();
List<String> list = new ArrayList<String>();
for(int i=0;i<set.size();i++){
list.add(getSectionDoubleBase(String.valueOf(i)));
}
return list;
}
- 读取properties配置文件的工具类
- properties配置文件的读取工具类
- 读取properties配置文件的工具类
- 读取properties配置文件的工具类
- 读取.properties配置文件工具类!!!
- 读取Properties配置文件工具类
- 读取配置文件properties工具类
- properties配置文件读取工具类
- 读取properties配置文件工具类
- 针对config.properties的配置文件读取的工具类
- XMLUtil工具类读取XML或properties格式的配置文件
- java 数据库读取工具类(读取config.properties配置文件)
- java 数据库读取工具类(读取config.properties配置文件)
- 配置文件properties的读取
- properties配置文件的读取
- 工具(一):properties配置文件读取工具类
- 用java自带的工具类ResourceBundle类读取.properties配置文件的工具类
- java读取项目里配置文件properties的工具
- 最新消息!Linux内核未来可能会引入形式验证
- vim中末行去掉^M
- JZOJ 3518. 【NOIP2013模拟11.6A组】进化序列(evolve)
- LELE的RPG难题 题解
- 04读书笔记之活动的启动模式
- 读取properties配置文件的工具类
- ionic angularjs $Resource 前端和nodejs后台配合的服务
- bzoj1117[POI2009]救火站Gas 贪心
- Keil综合(02)_工程窗口各项图标描述
- Rancher-k8s加速安装文档
- (一)java运算符
- Tensorflow——张量
- 求数列中两数异或合最大值 最富有的人 trie树贪心
- 简易商品购物系统登陆界面