Configuration
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的更多相关文章
- 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 ...
- Apache2.4:AH01630 client denied by server configuration
问题说明:Apache服务总共有4个,是为了防止单点故障和负载均衡,负载均衡控制由局方的F5提供. 访问的内容在NAS存储上,现象是直接访问每个apache的服务内容都是没有问题,但是从负载地址过来的 ...
- Fedora 22中的Locale and Keyboard Configuration
Introduction The system locale specifies the language settings of system services and user interface ...
- ABP源码分析四:Configuration
核心模块的配置 Configuration是ABP中设计比较巧妙的地方.其通过AbpStartupConfiguration,Castle的依赖注入,Dictionary对象和扩展方法很巧妙的实现了配 ...
- External Configuration Store Pattern 外部配置存储模式
Move configuration information out of the application deployment package to a centralized location. ...
- SCVMM中Clone虚拟机失败显示Unsupported Cluster Configuration状态
在SCVMM进行虚拟机的Clone,虽然失败了,但是Clone出虚拟机却显示在SCVMM控制台的虚拟机的列表中,并且状态是Unsupported Cluster Configuration.无法修复, ...
- commons configuration管理项目的配置文件
Commons Confifutation commons configuration可以很方便的访问配置文件和xml文件中的的内容.Commons Configuration 是为了提供对属性文件. ...
- Elasticsearch Configuration 中文版
##################### Elasticsearch Configuration Example ##################### # This file contains ...
- Spark 官方文档(4)——Configuration配置
Spark可以通过三种方式配置系统: 通过SparkConf对象, 或者Java系统属性配置Spark的应用参数 通过每个节点上的conf/spark-env.sh脚本为每台机器配置环境变量 通过lo ...
- spring configuration 注解
org.springframework.context.annotation @annotation.Target({ElementType.TYPE}) @annotation.Retention( ...
随机推荐
- Comparable接口
java.util.Arrays类也可以对Object数组进行排序,但是要使用这种方法排序必须实现Comparable接口,此接口就是用于指定对象排序规则的. 设计一个学生类,成绩由高到低排序,成绩相 ...
- MySQL学习笔记——约束
1.约束是在表上强制执行的数据检验规则,约束主要用于保证数据库的完整性. 2.当表中数据有相互依赖性时,可以保护相关的数据不被删除. 3.大部分数据库支持下面五类完整性约束: - NOT NULL非空 ...
- React入门简单实践
参考文献: 1.React入门示例教程——阮一峰 2.React仅仅只是你的界限 React主要的优点就是增量更新(虚拟DOM)和组件化(状态机). <!DOCTYPE html> < ...
- 第4章 jQuery的事件和动画(二)
二. jQuery中的动画 动画在前面几章案例中是回避不了的问题.此处结合一些简便的写法稍作系统的分析. 1. show()和hide()(1)介绍——不用过多的介绍了jQuery最基本的方法.本质是 ...
- C++ wMA 算法
CMakeLists.txt project(aaa) add_library(aaa SHARED aaa.cpp) add_executable(bbb bbb.cpp) target_link_ ...
- php的if语句单行与多行
//正确: //错误写法 $b = //if前面不能带 等号
- OC-protocol
一.简单使用 1. 基本用途 可以用来声明一大堆方法(不能声明成员变量) 只要某个类遵守了这个协议,就相当于拥有这个协议中的所有方法声明 只要父类遵守了某个协议,就相当于子类也遵守了 2. 格式 协议 ...
- 浅谈JavaScript中的事件
引言 Html页面与JavaScript之间的交互是通过事件来完成的.事件,就是文档或者浏览器窗口中发生的一些特定的交互瞬间.可以使用侦听器(处理程序)来预订事件,以便事件发生时执行相应的代码.这在传 ...
- CentOS7挂载分区教程
http://www.centoscn.com/CentOS/config/2014/1016/3955.html
- WinSCP 连接 Ubuntu 拒绝的问题
1.打开配置文件 $ sudo vi /etc/ssh/sshd_config 2.修改操作 PermitRootLogin without-password 修改为 PermitRootLogin ...