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. Flask-WTF form doesn't have attribute 'validate_on_submit'问题

    今天在学习WTF表单的时候遇到了这个问题,在stackoverflow上搜索查到了解决方案 from flask.ext.wtf import Form from wtforms import Tex ...

  2. 如何用Nsight调试C# OpenGL程序

    https://devtalk.nvidia.com/default/topic/804306/nsight-visual-studio-edition/nsight-4-5-can-t-debug- ...

  3. 几个有用的jQuery代码片段

    1.检测Internet Explorer版本 $(document).ready(function() { if (navigator.userAgent.match(/msie/i) ){ ale ...

  4. MBProgressHUD上传照片进度提示

    第一步,控制器先来个属性 @property (strong, nonatomic) MBProgressHUD *HUD; 第二步,显示与隐藏的调用方法 - (void)hudTipWillShow ...

  5. python 函数基础介绍

    函数是对程序逻辑进行结构化或过程化的一种编程方法.能将整块代码巧妙地隔离成易于管理的小块,把重复代码放在函数中而不是进行大量的拷贝. 一.函数创建 def 函数创建格式如下: def function ...

  6. Multiple actions were found that match the request Web API

    在WebAPI工程入口不对外公开的接口不能使用public. [HttpPost] public string PostRequest([FromBody] Model model) { /// } ...

  7. MYSQL的锁介绍,以及死锁发生情况-带例子

    mysql锁能在并发情况下的mysql进行更好的优化 MySQL有三种锁的级别:页级.表级.行级,这3种锁的特性可大致归纳如下: 表级锁:开销小,加锁快:不会出现死锁:锁定粒度大,发生锁冲突的概率最高 ...

  8. Xcode常用技巧(1)-使用Xcode进行代码分析及GDB调试

    1. 使用Xcode分析代码,分析分为静态分析及动态分析 静态分析:(Xcode-Product-Analyze)  检测代码是否有潜在的内存泄露  编译器认为不太合适的代码 运行结果: 若程序有 ...

  9. thinkphp 完整配置config.php

    ThinkPHP的系统配置都采用数组形式,配置文件地址:Bin/Conf/config.php CHECK_FILE_CASE -- windows环境下面的严格检查大小写.     /* 项目设定 ...

  10. [译]Node.js Best Practices

    原文: http://blog.risingstack.com/node-js-best-practices/ 下面的的最佳实践分为代码风格和开发工作流两种. 代码风格 Callback约定 Modu ...