Java-Properties文件读取工具类
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文件读取工具类的更多相关文章
- properties文件读取工具类
项目中防止硬编码,经常会将一些与业务相关的数据存在properties文件中,根据不同的key加载不同的数据,所以就会有读取properties文件的东西,这篇文章仅为了以后copy方便写的. 1.添 ...
- properties文件读写工具类
java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...
- Java 压缩文件夹工具类(包含解压)
依赖jar <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons ...
- 文件读取工具类读取properties文件
1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...
- properties文件读写工具类PropertiesUtil.java
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- 【代码】ini 文件读取工具类
using System; using System.Runtime.InteropServices; using System.Text; namespace hrattendance.Common ...
- XML文件读取工具类
/// <summary> /// Author: jiangxiaoqiang /// </summary> public class XmlReader { //===== ...
- FileUtils删除文件的工具类
前提是知道文件在哪个文件夹下面然后到文件夹下面删除文件,如果文件夹也需要传参数需要对下面方法进行改造. ( 需要借助于commons-io.jar和ResourceUtils.java ) 1.De ...
- SpringMVC中properties文件读取
SpringMVC给我们提供了用于properties文件读取的类: org.springframework.context.support.ResourceBundleMessageSource 1 ...
随机推荐
- spring容器启动
1 主要类 ContextLoaderListener:注册在web.xml中,web应用启动时,会创建它,并回调它的initWebApplicationContext()方法,从而创建并启动spri ...
- Falcon
1. JE falcon还需要安装je用来处理jdbc,否则打不开falcon的页面,爆内部错误503,然后看异常信息:Caused by: org.apache.falcon.FalconExcep ...
- ecmall公告挂件分析(转)--此挂件写法已有更新的写法。
ecmall的首页,基本上都是由挂件的形式实现的.ecmall所有的挂件程序,都在external\widgets文件下面.ecmall首页公告的插件,就是notice目录里面. 分析里面文件,con ...
- Requirejs简单介绍
具体详情请进入官网查阅:http://requirejs.org 一.什么是Requirejs RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一. 二. ...
- 关于python urlopen 一个类似radio流的timeout方法
终极解决方法来啦!看代码感受: import requests import eventlet import time eventlet.monkey_patch() try: with eventl ...
- JAVA课程设计(坦克大战)
2019-01-16 坦克大战游戏背景: 1. 需求分析 1.1环境要求 操作系统:Windows 7(SP1)以上 JAVA虚拟机:JDK1.8以上 开发环境:Eclipse(4.5以上) 1.2角 ...
- (转)Inno Setup入门(十)——操作注册表
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250871 有些程序需要随系统启动,或者需要建立某些文件关联等问题 ...
- idea 类注释,方法注释设置
类头注释:打开file->setting->Editor->Filr and Code Templates->Includes->File Header 直接在右边的文件 ...
- canvas之抒写文字
<canvas id="canvas" width="500" height="400" style="background ...
- Jquery 页面初始化常用的三种方法以及Jquery 发送ajax 请求
第一种 $(document).ready(function(){ //文档就绪事件 }); 第二种是第一种的简略写法,效果上和第一种是等效的. $(function(){ //文档加载事件,整个文档 ...