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 /*----------------------- ...
随机推荐
- Mac Anaconda 安装
下载地址 https://www.anaconda.com/download/#macos 选择对应的python 版本 安装 一路下一步 安装后打开如下 呵呵,此处装完,我的python 环境又从3 ...
- 第 2 章 容器架构 - 007 - Docker 架构详解
Docker 的核心组件包括: Docker 客户端 - Client Docker 服务器 - Docker daemon Docker 镜像 - Image Registry Docker 容器 ...
- 日常英语---十四、Dolce & Gabbana cancels China show amid 'racist' ad controversy(adj.温柔的,prep.在其中)
日常英语---十四.Dolce & Gabbana cancels China show amid 'racist' ad controversy(adj.温柔的,prep.在其中) 一.总结 ...
- php如何控制客户端生成缓存
php如何控制客户端生成缓存 一.总结 一句话总结:用http消息响应头中的Cache-Control来控制客户端缓存,说的是页面本身被客户端缓存,而不是重新生成的其它的非页面缓存 响应头Cache- ...
- go 圣经阅读笔记之-入门
go 圣经 这本书英文名为 <The Go Programming Language> 1. 简单hello world示例 helloworld.go package main impo ...
- python 读写TXT,安装pandas模块。
今天需要用python读TXT 文件,发现pandas库好用,所以就去下载,没想pythoncharm中的setting中下载失败,所以去下源文件,安装pandas 是提示得先装numpy库,于是又去 ...
- Practical Vim 第一章 & 第二章
第一章:Vim 解决问题的方式 前言 本质上讲,我们的工作是重复性的.凡是可以简化重复性操作的方式,都会成倍地节省我们的时间. Vim 对重复性操作进行了优化.它之所以能高效地重复,是因为它会记录我们 ...
- BGP - 4,BGP的三张表
1,BGP的三张表 邻居表(adjancy table) BGP表(forwarding database):BGP默认不做负载均衡,会选出一条最优的,放入路由表 路由表 ...
- 静默安装/ 普通安装与root权限获取相关
静默安装 有时候使用第三方的插件时我们需要静默安装其提供的apk包,静默安装时我们需要获取root权限,如下代码 Process process = Runtime.getRuntime().exec ...
- source code spark
http://blog.csdn.net/pelick/article/category/1556747 http://www.cnblogs.com/hseagle/