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 ...
随机推荐
- 11.Python使用Scrapy爬虫小Demo(新手入门)
1.前提:已安装好scrapy,且已新建好项目,编写小Demo去获取美剧天堂的电影标题名 2.在项目中创建一个python文件 3.代码如下所示: import scrapy class movies ...
- Android 从上层到底层-----kernel层
CPU:RK3288 系统:Android 5.1 功能:上层 app 控制 led 亮灭 开发板:Firefly RK3288 1.在dts文件中增加 led 设备 path:kernel/arch ...
- C/S模式与B/S模式的详细介绍
网络程序开发的两种计算模式--C/S模式与B/S模式.两种各有千秋,用于不同场合. C/S适用于专人使用,安全性要求较高的系统: B/S适用于交互性比较频繁的场合,容易被人们所接受,倍受用户和软件开发 ...
- Unit07: document 对象 、 自定义对象 、 事件
Unit07: document 对象 . 自定义对象 . 事件 知识点: <!DOCTYPE html> <html> <head> <meta chars ...
- mysql工作流程
1.connector sql交互语言,php,java等 2.系统管理和控制工具 3.连接池 管理缓冲用户连接,线程处理等需要缓存的需求 4.Sql接口接受sql命令,返回查询结果 5.解释器 sq ...
- JFreeChart API 说明(转)
原地址 http://blog.csdn.net/mike_caoyong/article/details/7338160 JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形 ...
- JavaScript笔记——使用AJax
在使用过JQuery之后,再来看JavaScript的Ajax实现就会觉得很麻烦,不过,最近使用到了,就记录一下吧 在JavaScript中Ajax的实现可以分为四步: 第一步 得到XMLHttpRe ...
- web前端 ajax加载动态生成复选框demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Linux 之不同运维人员共用root 账户权限审计
一.为什么? 在中小型企业,公司不同运维人员基本都是以root 账户进行服务器的登陆管理,缺少了账户权限审计制度.不出问题还好,出了问题,就很难找出源头. 这里介绍下,如何利用编译bash 使不同的客 ...
- 使用java获取自己的机器网卡
package org.ibase4j.core.util; import java.io.BufferedReader;import java.io.IOException;import java. ...