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. Java中的静态方法能否被重写

    能重写,但没有多态:https://blog.csdn.net/ghgzczxcvxv/article/details/43966243

  2. git 修改提交说明 commit message

    修改最近一次的提交说明 1.代码未推送到远程服务器 $ git commit --amend 此指令会打开文本编辑器,第二行就是提交说明,修改完后按 ctrl+x 退出,后面根据提示操作. 2.代码已 ...

  3. VS2003在vista/win7下搜索会出现僵死

    1.  VS2003在vista下搜索关键词的时候会出现僵死的问题的解决方案: VS2003快捷方式右击选中属性->兼容性页签 : 选中用兼容模式运行这个程序,下拉框中用windows xp2 ...

  4. STL传递比较函数进容器的三种方式

    对于STL中的依靠比较排序的容器,均提供了一个模板参数来传递比较函数,默认的为std::less<>. 查阅Containers - C++ Reference可以看到典型的使用比较函数的 ...

  5. jmeter数据关联_后置处理器_正则表达式提取器

  6. 十四 关于interrupt, interrupted, isInterrupted

    1 判断线程是否是停止状态? interrupt() : interrupt方法用于中断线程.调用该方法的线程的状态为将被置为"中断"状态. 注意:线程中断仅仅是置线程的中断状态位 ...

  7. 使用wifi网卡笔记1----网卡选型、开发环境搭建、内核配置

    1.wifi的STA模式和AP模式 Ap(Access Point)模式指的是可以将网卡设置为路由器用来共享流量或有线网络给别人使用, sta模式指的是当做网卡连接路由器上网 (1):AP也就是无线接 ...

  8. Python代码审计中一些需要重点关注的项

    SQL注入: 如果是常规没有进行预编译,或者直接使用原生的进行拼凑,那么在view的时候就需要多去观察了 [PythonSQL预编译]https://www.cnblogs.com/sevck/p/6 ...

  9. 用js如何获取一个上传文件的扩展名

    function suffix(file_name){     var result =/\.[^\.]+/.exec(file_name);     return result; }

  10. Halcon中循环读取文件的实现以及数字与字符的转换

    在循环读取文件的位置时,常用到数字与字符的转换. 数字与字符的转换 将字符转换为数字 tuple_number(StringImageIndex,IntImageIndex)` 1 2 1 2 将数字 ...