PropertiesUtil 获取文件属性值
有时候不要把一些属性值写死在代码中,而是写在配置在文件中,方便更改
PropertiesUtil工具类:读取key-value形式的配置文件,根据key获得value值
1、测试类
public class Test{
private static PropertiesUtil propertiesUtil = new PropertiesUtil("file.properties");
//根据文件中的key获取value值
String value = propertiesUtil.getStringProperty("文件中的key");
}
2、PropertiesUtil.java工具类
package com.util; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Properties;
import java.util.Set; import org.apache.log4j.Logger; public class PropertiesUtil { private static final Logger LOGGER = Logger.getLogger(PropertiesUtil.class); private final Properties props; public PropertiesUtil(final Properties props) {
this.props = props;
} public PropertiesUtil(final String propertiesFileName) {
final Properties properties = new Properties();
InputStreamReader in = null;
try {
in = new InputStreamReader(new FileInputStream(this.getClass().getResource("/").getPath()+propertiesFileName), "UTF-8");
/*
* 获取当前工程根目录
* in = new InputStreamReader(new FileInputStream(System.getProperty("user.dir") + File.separator + propertiesFileName), "UTF-8");
*/
properties.load(in);
} catch (final IOException ioe) {
LOGGER.error("Unable to read " + propertiesFileName, ioe);
} finally {
if (in != null) {
try {
in.close();
} catch (final IOException ioe) {
LOGGER.error("Unable to close " + propertiesFileName, ioe);
}
}
}
this.props = properties;
} public String getStringProperty(final String name) {
return props.getProperty(name);
} public int getIntegerProperty(final String name, final int defaultValue) {
String prop = props.getProperty(name);
if (prop != null) {
try {
return Integer.parseInt(prop);
} catch (final Exception ignored) {
return defaultValue;
}
}
return defaultValue;
} public long getLongProperty(final String name, final long defaultValue) {
String prop = props.getProperty(name);
if (prop != null) {
try {
return Long.parseLong(prop);
} catch (final Exception ignored) {
return defaultValue;
}
}
return defaultValue;
} public float getFloatProperty(final String name,final float defaultValue)
{
String prop = props.getProperty(name);
if (prop != null) {
try {
return Float.parseFloat(prop);
} catch (final Exception ignored) {
return defaultValue;
}
}
return defaultValue;
} public String getStringProperty(final String name, final String defaultValue) {
final String prop = getStringProperty(name);
return (prop == null) ? defaultValue : prop;
} public boolean getBooleanProperty(final String name) {
return getBooleanProperty(name, false);
} public boolean getBooleanProperty(final String name, final boolean defaultValue) {
final String prop = getStringProperty(name);
return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
} public Set<Object> keySet()
{
return props.keySet();
} public Collection<Object> values()
{
return props.values();
} }
PropertiesUtil 获取文件属性值的更多相关文章
- 获取.properties配置文件属性值
public class TestProperties { /** * * @Title: printAllProperty * @Description: 输出所有配置信息 * @param pro ...
- Linux 获取文件属性
使用stat/lstat获取文件属性 头文件:#include <sys/types.h> #include <sys/stat.h> int stat(const char ...
- Java--FutureTask原理与使用(FutureTask可以被Thread执行,可以被线程池submit方法执行,并且可以监控线程与获取返回值)
package com; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; i ...
- jquery 获取一组元素的选中项 - 函数、jquery获取复选框值、jquery获取单选按钮值
做表单提交时,如果现在还在用form提交,用户体验很差,所以一般使用ajax提交. 其中需要获取每个表单输入元素的值,获取的时候像文本框这些还好说,Jquery提供了 .val() 方法,获取很方便, ...
- 获取枚举值上的Description特性说明
/// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...
- jquery 获取设置值、添加元素详解
jQuery 获取内容和属性 jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易 ...
- 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值
接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...
- iframe无刷新跨域上传文件并获取返回值
通常我们会有一个统一的上传接口,这个接口会被其他的服务调用.如果出现不同域,还需要无刷新上传文件,并且获取返回值,这就有点麻烦了.比如,新浪微博启用了新域名www.weibo.com,但接口还是使用原 ...
- JQuery判断radio是否选中,获取选中值
本文摘自:http://www.cnblogs.com/xcj1989/archive/2011/06/29/JQUERY_RADIO.html /*----------------------- ...
随机推荐
- SVN下载项目导入到eclipse中出现错误解决办法:
首先要确定settings.xml配置路径正确 (下面是我自己的路径,设置自己的路径) 用客户端暴力解决方法: 1)把本地中工作空间中内容删除重新下载 2)导入到eclipse中 会出现一些问题 右键 ...
- Asp.net core 学习笔记 ( OData )
2018-12-10 更新 : 从前我都是把 entity 直接用于 odata 曝露 api 给程序用. 如果这个程序是我们自己写的前端,这样的方式非常好,因为就好比前端可以直接对数据库每一个表做操 ...
- 20170912xlVBA批量导入txt文件
Public Sub BatchImportTextFiles() AppSettings 'On Error GoTo ErrHandler Dim StartTime, UsedTime As V ...
- climbing stairs leetcode java
问题描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- 『算法设计_伪代码』贪心算法_最短路径Dijkstra算法
Dijkstra算法实际上是一个贪婪算法(Greedy algorithm).因为该算法总是试图优先访问每一步循环中距离起始点最近的下一个结点.Dijkstra算法的过程如下图所示. 初始化 给定图中 ...
- 漏洞复现——apache文件解析漏洞
漏洞描述: 我们可以上传一个文件名末尾包含换行符的文件,以此绕过它的黑名单 影响版本: apache 2.4.0-2.4.29 漏洞分析: <FilesMath "\.(?i:php| ...
- IE的“浏览器模式”和“文档模式的区别”
1.浏览器模式 用于切换IE针对该网页的默认文档模式.对不同版本浏览器的条件备注解析.发送给网站服务器的用户代理(User_Agent)字符串的值.网站可以根据浏览器返回的不同用户代理字符串判断浏览器 ...
- windows开启Apache的mod_rewrite模块
windows下安装apache默认是没有开启mod_rewrite模块的,启用也很简单,修改apache配置文件httpd.conf,查找rewrite_module, 找到这行:#LoadModu ...
- rpc框架实现(持续更新)
网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,rpc基于长连接的远程过程调用应用而生. 一:A服务调用B服务,整个调用过程,主要经历如下几个步骤:(摘自 ...
- os模块,序列化模块,json模块,pickle模块
一.os模块os.system("bash command") 运行shell命令,直接显示 os.popen("bash command).read() 运行shell ...