JAVA访问配置文件总结
2015-08-07 16:43
260 查看
一、全局配置的简单 propertie 文件实现
访问 properties 文件
更新中。。。
package com.testgs.utils; import java.util.*; import java.io.*; public final class ARConfig { private Properties conf = new Properties(); private String prefix = ""; /** * 全局配置文件名 */ public static final String GLOBAL_CONF_FILE = "/analysisReportConfig.properties"; public ARConfig(String prefix) { this.prefix = prefix; loadConf(); } /** * 取得属性文件实例 * @param prefix 各数据库连接前缀 * @return */ public synchronized static ARConfig getInstance(String prefix) { return new ARConfig(prefix); } public String getConfString(String name, String defaultValue) { String result = getConfString(name); result = (result == null) ? defaultValue : result; return result; } /**读取配置信息的 boolean 值 * @param name * @param defaultValue * @return */ public boolean getConfBoolean(String name, boolean defaultValue) { boolean result = defaultValue; String value = getConfString(name); if (value != null) { value = value.toLowerCase(); result = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); } return result; } /**读取配置信息的 boolean 值,如果没有,默认为 false * @param name * @return */ public boolean getConfBoolean(String name) { return getConfBoolean(name, false); } /** * 读取配置信息的 int 值 * @param name * @param defaultValue * @return */ public int getConfigInt(String name, int defaultValue) { String intV = getConfString(name); int result = defaultValue; if (intV != null) { try { result = Integer.parseInt(intV.trim()); } catch (Exception e) { e.printStackTrace(); } } return result; } public String getConfString(String name) { name = this.prefix + name; return conf.getProperty(name); } protected synchronized void loadConf() { conf.clear(); InputStream input = null; try { input = this.getClass().getResourceAsStream(GLOBAL_CONF_FILE); conf.load(input); } catch (IOException e) { throw new RuntimeException("找不到配置文件: " + GLOBAL_CONF_FILE); } finally { if (input != null) try { input.close(); } catch (Exception closeE) { } } } }
访问 properties 文件
更新中。。。
相关文章推荐
- springboot springjpa 支持多个数据源
- 一点一点学架构(四)—Spring.NET错误Cannot Resolve Type……
- SVN安装与配置 SVN整合MyEclipse
- java properties
- JAVA实现EXCEL行列号解析(一)——单格解析
- 引用第三方库So文件时,eclipse会自动把libs下arneabi文件里的so文件删除掉
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法
- Spring学习笔记(一)----理解IOC
- java泛型介绍及实例
- : Exception sending context initialized event to listener instance of class org.springframework.web.
- Java中Zip进行多文件保存
- Spring Security教程
- java反射详解
- eclipse 对齐行号在括号中显示和字体调整
- java同步方法和同步代码块详解(通俗地讲解)
- 转载:eclipse的访问控制图标详解
- Spring AOP 学习之cJlib动态代理
- java基础总结——异常
- JDK安装
- java 多线程读取多个文件 和 不用线程读取多个文件