有时候不要把一些属性值写死在代码中,而是写在配置在文件中,方便更改

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 获取文件属性值的更多相关文章

  1. 获取.properties配置文件属性值

    public class TestProperties { /** * * @Title: printAllProperty * @Description: 输出所有配置信息 * @param pro ...

  2. Linux 获取文件属性

    使用stat/lstat获取文件属性 头文件:#include <sys/types.h> #include <sys/stat.h> int stat(const char ...

  3. Java--FutureTask原理与使用(FutureTask可以被Thread执行,可以被线程池submit方法执行,并且可以监控线程与获取返回值)

    package com; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; i ...

  4. jquery 获取一组元素的选中项 - 函数、jquery获取复选框值、jquery获取单选按钮值

    做表单提交时,如果现在还在用form提交,用户体验很差,所以一般使用ajax提交. 其中需要获取每个表单输入元素的值,获取的时候像文本框这些还好说,Jquery提供了 .val() 方法,获取很方便, ...

  5. 获取枚举值上的Description特性说明

    /// <summary> /// 获取枚举值上的Description特性说明 /// </summary> /// <typeparam name="T&q ...

  6. jquery 获取设置值、添加元素详解

    jQuery 获取内容和属性 jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使访问和操作元素和属性变得很容易 ...

  7. 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值

    接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...

  8. iframe无刷新跨域上传文件并获取返回值

    通常我们会有一个统一的上传接口,这个接口会被其他的服务调用.如果出现不同域,还需要无刷新上传文件,并且获取返回值,这就有点麻烦了.比如,新浪微博启用了新域名www.weibo.com,但接口还是使用原 ...

  9. JQuery判断radio是否选中,获取选中值

    本文摘自:http://www.cnblogs.com/xcj1989/archive/2011/06/29/JQUERY_RADIO.html   /*----------------------- ...

随机推荐

  1. c# 读取txt方法

    string strLine; try { FileStream aFile = new FileStream("Log.txt", FileMode.Open); StreamR ...

  2. Backup and Recovery Types

    Physical(Raw) and Logical Backup: 1.Physical backups consist of raw copies of the directories and fi ...

  3. LeetCode--235--二叉树的最近公共祖先

    问题描述: 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的 ...

  4. java测试感想

    package ATM; public class Account { private String accountID; private String accountname; private St ...

  5. (GoRails) 使用ActiveStorage给user添加上传头像功能。

    对activestorage的简单使用: 头像库:uifaces.co. 可以使用大量设置好的头像图片. 1.安装avatar rails active_storage:install 2.user ...

  6. (GoRails)使用vue和Vuex管理嵌套的JavaScript评论, 使用组件vue-map-field

    嵌套的JavaScript评论 Widget Models 创建类似https://disqus.com/ 的插件 交互插件: Real time comments: Adapts your site ...

  7. Robot Movement(机器人移动)

    中文标题[机器人移动] 这个题目是 Kayak 发布的代码挑战题目. 我认为题目本身描述的不是十分清楚,方法需要返回结果,但是结果没有说明是机器人最后的坐标位置,还是最后的坐标位置距离原点的距离.同时 ...

  8. node初学者笔记

    helloworld 编辑一个js文件——在该文件所属目录打开命令行cmd——输入'node -v可查看版本——输入'node  00-hellowolrd.js(你的js名字)' 或者直接在文件所属 ...

  9. CentOS6.8下实现配置配额

    CentOS6.8下实现配置配额 Linux系统是支持多用户的,即允许多个用户同时使用linux系统,普通用户在/home/目录下均有自己的家目录,在默认状态下,各个用户可以在自己的家目录下任意创建文 ...

  10. VitrualBox、vagrant、homestead的关系

    VitrualBox 是一款非常强大的免费虚拟机软件,使用者可以在 VitrualBox 上安装并运行 Linux.Windows.Mac OS X 等操作系统,类似的软件还有 VMware Vagr ...