java-读properties配置文件
package com.test; import java.util.HashMap;
import java.util.Map;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.*; import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager; public class PropertyUtil { private static final String DEFAULT_PROPERTY = "conf/conf";
// private static final String DEFAULT_PROPERTY = "conf"; private static final String PROPERTY_SUFIX = ".properties"; private static final Map<String, PropertiesConfiguration> CONFIGURATIONS = new HashMap<String, PropertiesConfiguration>(); static private Logger logger = LogManager.getLogger(PropertyUtil.class); private static PropertiesConfiguration getInstance(String name)
{
String fileName = getPropertyName(name);
// logger.debug("filename=" + fileName);
PropertiesConfiguration config = CONFIGURATIONS.get(fileName);
if (config == null)
{
try
{
// logger.debug("congfig is null"); config = new PropertiesConfiguration(fileName);
config.setReloadingStrategy(new FileChangedReloadingStrategy());
CONFIGURATIONS.put(fileName, config);
}
catch (ConfigurationException e)
{
logger.error("cannot find property file for : " + name);
throw new RuntimeException("cannot find property file for : " + name);
}
}
return config;
} private static String getPropertyName(String name)
{
if (StringUtils.isBlank(name))
{
name = DEFAULT_PROPERTY;
}
return name.endsWith(PROPERTY_SUFIX) ? name : (name += PROPERTY_SUFIX);
} public static PropertiesConfiguration getInstance()
{
return getInstance(null);
} public static String getString(String key)
{
return getString(key, DEFAULT_PROPERTY);
} public static String getString(String key, String propertyInstance)
{
return getInstance(propertyInstance).getString(key);
} public static int getInt(String key)
{
return getInt(key, DEFAULT_PROPERTY);
} public static int getInt(String key, String propertyInstance)
{
String value = getInstance(propertyInstance).getString(key);
return Integer.parseInt(value);
}
public static void main(String[] args)
{
PropertyUtil test = new PropertyUtil();
System.out.print(test.getString("key"));
} }
java-读properties配置文件的更多相关文章
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- 【转】Java 读写Properties配置文件
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
- Java 读写Properties配置文件
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
- Java读properties文件中文乱码问题的解决方法
java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...
- java读properties文件 乱码
java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...
- java读取properties配置文件信息
一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...
- java读properties的通用类,兼容linux和windows
package util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; / ...
- java读取properties配置文件的方法
app.properties mail.smtp.host=smtp.163.com mail.transport.protocol=smtp import java.io.InputStream; ...
- Java 读Properties
import java.io.*; import java.util.Properties; public class Study { public static void main(String[] ...
- Java 获取*.properties配置文件中的内容 ,常见的两种方法
import java.io.InputStream; import java.util.Enumeration; import java.util.List; import java.util.Pr ...
随机推荐
- UVA 10943 How do you add? DP
Larry is very bad at math — he usually uses a calculator, whichworked well throughout college. Unfor ...
- sql 泡沫 或者 递归查询
if object_id('[tb]') is not null drop table [tb] go ),parentid int) insert [tb] ,N union all ,N unio ...
- stringbuffer与stringbuilder的区别?
1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与StringBuilder,他们是字符串变量,是可改变的对象,每当我们用它们 ...
- 开启MySQL慢查询日志
1.修改my.cnf或my.ini 1).linux----------------------------------- /etc/my.cnf 文件 [mysqld] long_query_ti ...
- python中写shell(转)
python中写shell,亲测可用,转自stackoverflow To run a bash script, copy from stackoverflow def run_script(scri ...
- Backbone seajs demo2
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- webServer xampp的安装及使用
一.下载: 西装xampp1.73版本,其他版本可能会出错,为了你能顺利安装,最好用此版本: 二.安装: 1.安装路径,最好不要放到系统盘去[c:],建议非系统盘都可,尤其是早期的XAMPP版本可能默 ...
- 一个日期Js文件。 2013年10月12日 星期六 癸巳年九月初八
1.简单用法 <div align="center"> <SCRIPT language=JavaScript src="js/calendar.js ...
- Android eclipse中程序调试
一:断点调试 用eclipse开发android程序的时,跟VS一样是可以断点单步调试的.步骤如下.1 设置断点:在编码窗体的左边框上用鼠标双击,或者右键点击菜单,选择 Toggle Breakpoi ...
- 南阳理工ACM 括号匹配问题,并求出使得括号能够匹配需要新增的最小括号数(括号匹配(二))
描述 给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起 ...