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

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. top 命令常用操作

    1.显示进程参数 top -c 下面操作为top命令后操作 2.按cpu/mem排序 shift + p/m 3.高亮 排序列 按x键 4.高亮 变化进程行 按b键 5.显示cpu每核的运行状态 按1 ...

  2. 异常上报工具:腾讯Bugly

    1.腾讯出了一个和umeng差不多的异常上报工具Bugly.(传送门:https://bugly.qq.com/docs/) (1)两者比较明显的区别是,Bugly能比较实时上报异常信息,经过测试基本 ...

  3. Codeforces 937D - Sleepy Game

    937D - Sleepy Game 思路: dfs. vis[u][0]==1表示u这个点能从s点偶数路径到达 vis[u][1]==1表示u这个点能从s点奇数路径到达 这个样就能保证dfs时每个点 ...

  4. Angular 学习笔记 (version 6 小笔记)

    1. lazyload 的 path 变成相对路径了, 不过如果你用 ng update 的话, 依然可以不需要修改, cli config 好像能调支持绝对路径的写法. const routes: ...

  5. 最简单的网络图片的爬取 --Pyhon网络爬虫与信息获取

    1.本次要爬取的图片的url http://www.nxl123.cn/static/imgs/php.jpg 2.代码部分 import requestsimport osurl = "h ...

  6. Struts2框架之类型转换 --Struts2框架

    Struts2框架实现了大多数常见的用于类型转换的转换器,开发人员不用自己编写类型转换代码,就可以实现数据类型的转换.下面一个Struts2框架类型转换的简单事例, 本例可在使用validate()方 ...

  7. Vue音乐项目笔记(五)

    1.搜索列表的点击删除.删除全部的交互事件 https://blog.csdn.net/weixin_40814356/article/details/80496097 seach组件中放search ...

  8. 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)

    题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...

  9. centos命令行系列之centos6防火墙的关闭以及开启

    输入:cat /etc/issue   查看版本 (一)通过service命令 注:service命令开启以及关闭防火墙为即时生效,下次重启机器的时候会自动复原 查看防火墙状态:service ipt ...

  10. SpringCloud 将服务注册到Eureka Server上

    提供好服务生产者: 1.添加spring-cloud-starter-eureka依赖 <dependencyManagement> <dependencies> <de ...