Java J2EE读取配置文件
来源:互联网 发布:qq 微信 知乎 编辑:程序博客网 时间:2024/05/16 02:32
package com;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.naming.InitialContext;import java.io.File;import java.io.FileFilter;import java.io.FileReader;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Properties;import java.util.concurrent.ConcurrentHashMap;/** * 读取配置文件 */public class ReadProperty {private static final Logger logger = LoggerFactory.getLogger(Globals.class);private static String confHome = null;//并发,线程安全的mapprivate static Map<String, String> confProperties = new ConcurrentHashMap<>();private static Map<String, File> confFiles = new ConcurrentHashMap<>();//加载成功private static boolean loadingSuccess = true; /** * 加载配置文件 */private synchronized static void loadProperties() {//如果没有加载成功,返回if ( !loadingSuccess ) { return;}//如果加载的文件是空的if (confProperties.isEmpty()) {//如果电脑环境变量中为空if (confHome == null) { confHome = System.getProperty("CONF_HOME"); } //如果web.xml中没配 if (confHome == null) { try { InitialContext context = new InitialContext(); confHome = (String)context.lookup("java:comp/env/CONF_HOME"); } catch(Exception e) { logger.warn("Can not find jini name {}", "java:comp/env/CONF_HOME"); } } //如果还是为空,就找本机路径下的ProtectionDomain/CodeSource/Location/getFile/WEB-INF/文件夹/conf if (confHome == null) { confHome = (new InitWebPath()).getRootPath() + "WEB-INF" + File.separator + "conf"; } //是否是文件夹 try { File dirFile = new File(confHome); if(!dirFile.exists() || (!dirFile.isDirectory())){ logger.warn("Can not find home or is not directory!\n{}", confHome); loadingSuccess = false; return; } //获取所有文件后缀是.properties的文件名 File[] files = dirFile.listFiles(new FileFilter() { @Override public boolean accept(File file) { String fileName = file.getName(); int pos = fileName.lastIndexOf(".properties");//最后一个匹配的 db.xml和db.xml.xml if (pos != -1) { confFiles.put(fileName.substring(0, pos), file);//文件名与文件关联 key value return true; } else { pos = fileName.lastIndexOf(".xml"); confFiles.put(fileName.substring(0, pos), file); return false; } } } ); //迭代文件,读取key value for(File file : files) { Properties fileProperties = new Properties(); fileProperties.load(new FileReader(file)); Iterator<Entry<Object, Object>> iterProp = fileProperties.entrySet().iterator(); while(iterProp.hasNext()) { Entry<Object, Object> row = iterProp.next(); Object key = row.getKey(); Object value = row.getValue(); if (null!=key && null!=value) { confProperties.put(key.toString(), value.toString()); } } } } catch(Exception e) { loadingSuccess = false; }}}/** * 读取配置文件信息 * @param name key * @return value */public static String getProperty(String name) {if (confProperties.isEmpty()) {loadProperties();}return confProperties.get(name);}static class InitWebPath{public String getRootPath() {String url = InitWebPath.class.getProtectionDomain().getCodeSource().getLocation().getFile();String filePath = "";try {filePath = java.net.URLDecoder.decode(url, "utf-8");} catch (Exception e) {logger.error(e.getMessage(), e);}final String fileFlag = "file:";if (filePath.startsWith(fileFlag)) {filePath = filePath.substring(fileFlag.length());}final String applicationFlag = "WEB-INF";return filePath.substring(0, filePath.lastIndexOf(applicationFlag));}}}
阅读全文
0 0
- Java J2EE读取配置文件
- java读取配置文件
- java读取配置文件
- java 读取properties配置文件
- Java读取properties配置文件
- JAVA读取配置文件路径
- java读取xml配置文件
- JAVA读取配置文件
- Java读取配置文件
- Java配置文件读取
- java读取xml配置文件
- java读取properties配置文件
- java 读取配置文件properties
- java读取配置文件
- Java读取配置文件
- java读取配置文件
- java读取配置文件property
- java 读取*.properties配置文件
- 掀开<mvc:annotation-driven/>的面纱
- WEB应用中的SESSION详解
- 常用脚本(持续更新)
- node.js实现用户登录注册简单示例
- 浅析SDCycleScrollView实现原理
- Java J2EE读取配置文件
- C#实现KNN算法
- weex服务项目搭建
- Cordova插件调用Android原生Activity修改其样式及背景
- 系统相关功能开发(四)-应用程序操作
- yum源在执行yum update 之后无法使用:Could not retrieve mirrorlist
- myeclpse9 导入maven项目不出现选项的问题
- Android7.0及以上相机相册调用闪退处理封装,向下兼容
- 基于Qt的图像处理技术和算法