import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; /**
* @ClassName: ReadPropertiesUtil
*/
public class ReadPropertiesUtil {
// 声明配置文件
private static String[] confProps = { "config.properties" };
private static PropertiesConfiguration conf = null; /**
* 获取所有配置对象
*/
public static PropertiesConfiguration getConf() {
conf = new PropertiesConfiguration();// 初始化对象
/* 把所有props配置文件一次加载,共后续使用 */
for (int i = 0; i < confProps.length; i++) {
try {
conf.load(confProps[i]);
} catch (ConfigurationException e) {
conf = null;
}
}
return conf;
} /*
* PropsUtil getInt()
*/
public static Integer getInt(String key) {
/** 声明返回值 **/
Integer value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getInt(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getString()
*/
public static String getString(String key) {
/** 声明返回值 **/
String value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getString(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getLong()
*/
public static Long getLong(String key) {
/** 声明返回值 **/
Long value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getLong(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getBoolean()
*/
public static boolean getBoolean(String key) {
/** 声明返回值 **/
boolean value = true;
try {
/* 获取value值 */
conf = getConf();
value = conf.getBoolean(key);
} catch (Exception e) {
value = false;
}
return value;
} /*
* PropsUtil getList()
*/
public static String[] getStringArray(String key) {
/** 声明返回值 **/
String[] value = null;
try {
/* 获取value值 */
conf = getConf();
value = conf.getStringArray(key);
} catch (Exception e) {
}
return value;
} /*
* PropsUtil getJsonHeaders()
*/
public static JSONObject getJsonHeaders(String key) {
JSONObject json = null;// 声明返回值
try {
/** 解析*.Properties文件 **/
conf = getConf();
String value = conf.getString(key).replace("=", ",");
json = JSON.parseObject(value);
} catch (Exception e) {
}
return json;
} public static void main(String[] args) throws Exception { System.out.println(getString("dbcp.url")); // String[] tmp = getStringArray("Email_to");
// for(String to:tmp){
// System.out.println(to);
// } // JSONObject json = getJsonHeaders("httpHead");
// for(Entry<String, Object> entry:json.entrySet()){
// System.out.println(entry.getKey() + "-----" + entry.getValue().toString());
// } // System.out.println(getInt("dbcp.url")); // System.out.println(getInt("redis.minIdle"));
// String[] tmp = getStringArray("redis.redisSlaveIp");
// for(int i=0;i<tmp.length;i++){
// System.out.println(tmp[i].split(":")[0]);
// System.out.println(tmp[i].split(":")[1]);
// }
}
}
 //相关依赖
<!-- commons-configuration -->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<!-- fastjson json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>

Properties文件载入工具类:

  依赖spring等。

  

         <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
 import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Properties; import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; /**
* Properties文件载入工具类. 可载入多个properties文件,
* 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
*/
public class PropertiesLoader {
private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
private final Properties properties; public PropertiesLoader(String... resourcesPaths) {
properties = loadProperties(resourcesPaths);
} public Properties getProperties() {
return properties;
} /**
* 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
*/
public Boolean getBoolean(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Boolean.valueOf(value);
} /**
* 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
*/
public Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return value != null ? Boolean.valueOf(value) : defaultValue;
} /**
* 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
*/
public Double getDouble(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Double.valueOf(value);
} /**
* 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
*/
public Double getDouble(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Double.valueOf(value) : defaultValue;
} /**
* 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
*/
public Integer getInteger(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Integer.valueOf(value);
} /**
* 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
*/
public Integer getInteger(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Integer.valueOf(value) : defaultValue;
} /**
* 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
*/
public String getProperty(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return value;
} /**
* 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
*/
public String getProperty(String key, String defaultValue) {
String value = getValue(key);
return value != null ? value : defaultValue;
} /**
* 取出Property,但以System的Property优先,取不到返回空字符串.
*/
private String getValue(String key) {
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
if (properties.containsKey(key)) {
return properties.getProperty(key);
}
return "";
} /**
* 载入多个文件, 文件路径使用Spring Resource格式.
*/
private Properties loadProperties(String... resourcesPaths) {
Properties props = new Properties();
for (String location : resourcesPaths) {
InputStream is = null;
try {
Resource resource = resourceLoader.getResource(location);
is = resource.getInputStream();
props.load(is);
} catch (IOException ex) {
logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
} finally {
IOUtils.closeQuietly(is);
}
}
return props;
}
}

Java-Properties文件读取工具类的更多相关文章

  1. properties文件读取工具类

    项目中防止硬编码,经常会将一些与业务相关的数据存在properties文件中,根据不同的key加载不同的数据,所以就会有读取properties文件的东西,这篇文章仅为了以后copy方便写的. 1.添 ...

  2. properties文件读写工具类

    java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...

  3. Java 压缩文件夹工具类(包含解压)

    依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...

  4. 文件读取工具类读取properties文件

    1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...

  5. properties文件读写工具类PropertiesUtil.java

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  6. 【代码】ini 文件读取工具类

    using System; using System.Runtime.InteropServices; using System.Text; namespace hrattendance.Common ...

  7. XML文件读取工具类

    /// <summary> /// Author: jiangxiaoqiang /// </summary> public class XmlReader { //===== ...

  8. FileUtils删除文件的工具类

    前提是知道文件在哪个文件夹下面然后到文件夹下面删除文件,如果文件夹也需要传参数需要对下面方法进行改造. ( 需要借助于commons-io.jar和ResourceUtils.java  ) 1.De ...

  9. SpringMVC中properties文件读取

    SpringMVC给我们提供了用于properties文件读取的类: org.springframework.context.support.ResourceBundleMessageSource 1 ...

随机推荐

  1. python编程规范系列--建议01~07

    本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理. 本书主要内容     1)容易被忽视的重要概念和常识,如代码的布局和编写函数的原则等:     2)编写py ...

  2. SysTick_Config

    SystemCoreClockUpdate();SysTick_Config(SystemCoreClock/2000);   //500us

  3. Android screencap截屏指令

    查看帮助(注意:有的网友错误使用 screencap -v ,结果差不多,因为系统不能识别-v,就自动打印出帮助信息) # screencap -hscreencap -husage: screenc ...

  4. "==" 与 “equals”

    “==”: “==”或等号操作在Java编程语言中是一个二元操作符,用于比较原生类型和对象.(操作符不支持重载overloading) “==”对比两个对象基于内存引用,如果两个对象的引用完全相同(指 ...

  5. Linnx 服务器中mysql 无法正常访问问题

    本机连接远程Linnx服务器不通 1. 检测防火墙 -- 保证防火墙关闭 查看到iptables服务的当前状态:service iptables status. 但是即使服务运行了,防火墙也不一定起作 ...

  6. 数据结构与算法JavaScript描述——栈

    栈就是和列表类似的一种数据结构,它可用来解决计算机世界里的很多问题. 栈是一种高效的数据结构,因为数据只能在栈顶添加或删除,所以这样的操作很快,而且容易实现. 栈的使用遍布程序语言实现的方方面面,从表 ...

  7. eclipse插件-easy explore

    最近找到一个Eclipse的插件,名字是Easy Explore,是Easy Structs 其 中的一个部分.主要的功能就是在Eclipse里面视图的部分如果看到自己的工程,或者Package,包什 ...

  8. 浅谈PHP面向对象编程(七、抽象类与接口)

    7.0 抽象类与接口 当定义一个类时,常常需要定义一些方法来描述该类的行为特征.但有时这些方法的实现方式是无法确定的,此时就可以使用抽象类和接口. 抽象类和接口用于提高程序的灵活性.抽象类是一种特殊的 ...

  9. 线程queue、线程进程池、异步回调机制

    1. 线程 queue queue is especially useful in threaded programming when information must be exchanged sa ...

  10. MetaboAnalysis KEGG match methods

    1.KEGG regexdb.idmap.find({"name":{"$regex":"Prolyl-hydroxyproline",&q ...