package edu.fzu.ir.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties; import org.apache.log4j.Logger; /**
* 读取配置文件的工具类
* 提供了静态读取配置文件的方法
* 改配置文件是放在每个worker节点�?
* worker启动时根据配置文件路径读取该配置文件
* @author hasee
*
*/
public class Configuration {
private static Logger logger = Logger.getLogger(Configuration.class);
private static Properties properties;
private Configuration() {} /**
* 通过配置文件的路径获取相关的配置,存储在properties中,worker每次启动时调用该方法
* @param confPath 配置文件的路�?
*/
public static void loadConfiguration(String confPath) {
//判断properties是否是空,如果不为空表示已经加载过配置文件,不需要再加载
if(properties==null) {
//利用InputStreamReader指定利用utf-8编码读取properties文件
//这样是为了解决配置文件中的中文乱码问�?
InputStream inputStream = null;
InputStreamReader isr = null;
try {
inputStream = new FileInputStream(new File(confPath));
isr = new InputStreamReader(inputStream, "UTF-8");
} catch (FileNotFoundException e) {
//文件未找到时,输出错误日志,并停止系�?
logger.error(e);
System.exit(0);
} catch (UnsupportedEncodingException e) {
logger.error(e);
System.exit(0);
}
properties = new Properties();
try {
//加载并存储到properties
logger.info("load configuration from : "+ confPath);
properties.load(isr);
logger.info("load succeed!");
} catch (IOException e) {
logger.error(e);
} finally {
//关闭资源
try {
if(inputStream != null) {
inputStream.close();
}
if(isr != null) {
isr.close();
}
} catch (IOException e) {
logger.error(e);
}
}
}
} /**
* 获取配置文件中某�?��配置�?若不存在返回null
* @param key 配置项的key�?
* @return
*/
public static String getProperties(String key) {
String s = null;
if(properties != null){
s = properties.getProperty(key);
}
return s;
}
}

Configuration的更多相关文章

  1. PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.

    PhpStorm和WAMP配置调试参数 问题描述: Error. Interpreter is not specified or invalid. Press “Fix” to edit your p ...

  2. Apache2.4:AH01630 client denied by server configuration

    问题说明:Apache服务总共有4个,是为了防止单点故障和负载均衡,负载均衡控制由局方的F5提供. 访问的内容在NAS存储上,现象是直接访问每个apache的服务内容都是没有问题,但是从负载地址过来的 ...

  3. Fedora 22中的Locale and Keyboard Configuration

    Introduction The system locale specifies the language settings of system services and user interface ...

  4. ABP源码分析四:Configuration

    核心模块的配置 Configuration是ABP中设计比较巧妙的地方.其通过AbpStartupConfiguration,Castle的依赖注入,Dictionary对象和扩展方法很巧妙的实现了配 ...

  5. External Configuration Store Pattern 外部配置存储模式

    Move configuration information out of the application deployment package to a centralized location. ...

  6. SCVMM中Clone虚拟机失败显示Unsupported Cluster Configuration状态

    在SCVMM进行虚拟机的Clone,虽然失败了,但是Clone出虚拟机却显示在SCVMM控制台的虚拟机的列表中,并且状态是Unsupported Cluster Configuration.无法修复, ...

  7. commons configuration管理项目的配置文件

    Commons Confifutation commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件. ...

  8. Elasticsearch Configuration 中文版

    ##################### Elasticsearch Configuration Example ##################### # This file contains ...

  9. Spark 官方文档(4)——Configuration配置

    Spark可以通过三种方式配置系统: 通过SparkConf对象, 或者Java系统属性配置Spark的应用参数 通过每个节点上的conf/spark-env.sh脚本为每台机器配置环境变量 通过lo ...

  10. spring configuration 注解

    org.springframework.context.annotation @annotation.Target({ElementType.TYPE}) @annotation.Retention( ...

随机推荐

  1. Java——按钮组件:JButton

    import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Point; impor ...

  2. FreeImage使用

    http://blog.csdn.net/byxdaz/article/details/6056509 http://blog.chinaunix.net/uid-20660110-id-65639. ...

  3. XtraFinder在OSX10.11的使用

    重启系统,按住command键加上R键 进入恢复模式还是什么模式里,然后启动terminal 然后键入 csrutil enable --without debug 重启电脑,可正常使用 居然上传不了 ...

  4. 重写UILabler的sizeThatFits方法,需要触发两次才会有效果

    #import "ViewController.h" @interface SpecialLabel:UILabel @end @implementation SpecialLab ...

  5. bootloader

    1) C# 为了给设备升级固件,在前同事的基础上改了下,在.NET Framework下写的. 2)Tera Term + ttl 上面.NET平台的运行文件虽然小巧,但是依赖.NET Framewo ...

  6. WebGrid with filtering, paging and sorting 【转】

    WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...

  7. Simple colum formatting in Yii 2 GridView

    A very important widget in the business apps development is the GridView control. In this post I wil ...

  8. VisualStudio基本使用(1)-显示行号

    "工具"-"选项"-"文本编辑器"-"C/C++"-"常规",勾选"行号"复选框 ...

  9. 新浪微博客户端(47)-在TextView中插入表情

    DJEmotionPageView.m // 发送点击广播(和android类似,区别在于android的广播是只要有上下文对象context,就可以发送) // iOS中的通知发送和接收都是通过NS ...

  10. Linux下,telnet命令如何退出

    测试连接本地的memcached telnet 链接后是这样的: wangkongming@Vostro ~ $ telnet Trying 127.0.0.1... Connected to 127 ...