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——URLEncoder和URLDecoder

    import java.net.URLDecoder; import java.net.URLEncoder; //========================================== ...

  2. JSP 属性范围

    参考文献:http://www.cnblogs.com/xdp-gacl/p/3781056.html 一.属性范围 所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围 ...

  3. PHP 出现 502 解决方案

    原文:http://www.ahlinux.com/php/10319.html nginx+php 出现502 bad gateway,一般这都不是nginx的问题,而是由于 fastcgi或者ph ...

  4. winSocket数据传输

    服务器端: #include <WINSOCK2.H> #include <stdio.h> #pragma comment(lib,"ws2_32.lib" ...

  5. Sky Box

    http://www.keithlantz.net/2011/10/rendering-a-skybox-using-a-cube-map-with-opengl-and-glsl/ http://o ...

  6. throw er; // Unhandled 'error' event

    events.js:72 throw er; // Unhandled 'error' event ^Error: listen EADDRINUSE at errnoException (net.j ...

  7. Apache日志配置详解(rotatelogs LogFormat)

    logs/error_logCustomLog logs/access_log common--默认为以上部分 修改为如下: ErrorLog "|/usr/sbin/rotatelogs ...

  8. JS网页顶部进度条demo

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服 ...

  10. 深入理解计算机系统-从书中看到了异或交换ab两个值的新感

    还得从一个很经典的面试题说起:不通过第三个变量来交换两个变量a,b的值... 一个很经典的答案是通过异或来解决: 第壹步:a=a^b; 第贰步:b=a^b; 第叁步:a=a^b; 以前提起" ...