1. package com;
  2.  
  3. import org.slf4j.Logger;
  4.  
  5. import org.slf4j.LoggerFactory;
  6.  
  7. import javax.naming.InitialContext;
  8.  
  9. import java.io.File;
  10.  
  11. import java.io.FileFilter;
  12.  
  13. import java.io.FileReader;
  14.  
  15. import java.util.Iterator;
  16.  
  17. import java.util.Map;
  18.  
  19. import java.util.Map.Entry;
  20.  
  21. import java.util.Properties;
  22.  
  23. import java.util.concurrent.ConcurrentHashMap;
  24.  
  25. /**
  26.  
  27. * 读取配置文件
  28.  
  29. */
  30.  
  31. public class ReadProperty {
  32.  
  33. private static final Logger logger = LoggerFactory.getLogger(Globals.class);
  34.  
  35. private static String confHome = null;
  36.  
  37. //并发,线程安全的map
  38.  
  39. private static Map<String, String> confProperties = new ConcurrentHashMap<>();
  40.  
  41. private static Map<String, File> confFiles = new ConcurrentHashMap<>();
  42.  
  43. //加载成功
  44.  
  45. private static boolean loadingSuccess = true;
  46.  
  47. /**
  48.  
  49. * 加载配置文件
  50.  
  51. */
  52.  
  53. private synchronized static void loadProperties() {
  54.  
  55. //如果没有加载成功,返回
  56.  
  57. if ( !loadingSuccess ) {
  58.  
  59. return;
  60.  
  61. }
  62.  
  63. //如果加载的文件是空的
  64.  
  65. if (confProperties.isEmpty()) {
  66.  
  67. //如果电脑环境变量中为空
  68.  
  69. if (confHome == null) {
  70.  
  71. confHome = System.getProperty("CONF_HOME");
  72.  
  73. }
  74.  
  75. //如果web.xml中没配
  76.  
  77. if (confHome == null) {
  78.  
  79. try {
  80.  
  81. InitialContext context = new InitialContext();
  82.  
  83. confHome = (String)context.lookup("java:comp/env/CONF_HOME");
  84.  
  85. } catch(Exception e) {
  86.  
  87. logger.warn("Can not find jini name {}", "java:comp/env/CONF_HOME");
  88.  
  89. }
  90.  
  91. }
  92.  
  93. //如果还是为空,就找本机路径下的ProtectionDomain/CodeSource/Location/getFile/WEB-INF/文件夹/conf
  94.  
  95. if (confHome == null) {
  96.  
  97. confHome = (new InitWebPath()).getRootPath() + "WEB-INF" + File.separator + "conf";
  98.  
  99. }
  100.  
  101. //是否是文件夹
  102.  
  103. try {
  104.  
  105. File dirFile = new File(confHome);
  106.  
  107. if(!dirFile.exists() || (!dirFile.isDirectory())){
  108.  
  109. logger.warn("Can not find home or is not directory!\n{}", confHome);
  110.  
  111. loadingSuccess = false;
  112.  
  113. return;
  114.  
  115. }
  116.  
  117. //获取所有文件后缀是.properties的文件名
  118.  
  119. File[] files = dirFile.listFiles(new FileFilter() {
  120.  
  121. @Override
  122.  
  123. public boolean accept(File file) {
  124.  
  125. String fileName = file.getName();
  126.  
  127. int pos = fileName.lastIndexOf(".properties");//最后一个匹配的 db.xml和db.xml.xml
  128.  
  129. if (pos != -1) {
  130.  
  131. confFiles.put(fileName.substring(0, pos), file);//文件名与文件关联 key value
  132.  
  133. return true;
  134.  
  135. } else {
  136.  
  137. pos = fileName.lastIndexOf(".xml");
  138.  
  139. confFiles.put(fileName.substring(0, pos), file);
  140.  
  141. return false;
  142.  
  143. }
  144.  
  145. }
  146.  
  147. }
  148.  
  149. );
  150.  
  151. //迭代文件,读取key value
  152.  
  153. for(File file : files) {
  154.  
  155. Properties fileProperties = new Properties();
  156.  
  157. fileProperties.load(new FileReader(file));
  158.  
  159. Iterator<Entry<Object, Object>> iterProp = fileProperties.entrySet().iterator();
  160.  
  161. while(iterProp.hasNext()) {
  162.  
  163. Entry<Object, Object> row = iterProp.next();
  164.  
  165. Object key = row.getKey();
  166.  
  167. Object value = row.getValue();
  168.  
  169. if (null!=key && null!=value) {
  170.  
  171. confProperties.put(key.toString(), value.toString());
  172.  
  173. }
  174.  
  175. }
  176.  
  177. }
  178.  
  179. } catch(Exception e) {
  180.  
  181. loadingSuccess = false;
  182.  
  183. }
  184.  
  185. }
  186.  
  187. }
  188.  
  189. /**
  190.  
  191. * 读取配置文件信息
  192.  
  193. * @param name key
  194.  
  195. * @return value
  196.  
  197. */
  198.  
  199. public static String getProperty(String name) {
  200.  
  201. if (confProperties.isEmpty()) {
  202.  
  203. loadProperties();
  204.  
  205. }
  206.  
  207. return confProperties.get(name);
  208.  
  209. }
  210.  
  211. static class InitWebPath{
  212.  
  213. public String getRootPath() {
  214.  
  215. String url = InitWebPath.class.getProtectionDomain().getCodeSource().getLocation().getFile();
  216.  
  217. String filePath = "";
  218.  
  219. try {
  220.  
  221. filePath = java.net.URLDecoder.decode(url, "utf-8");
  222.  
  223. } catch (Exception e) {
  224.  
  225. logger.error(e.getMessage(), e);
  226.  
  227. }
  228.  
  229. final String fileFlag = "file:";
  230.  
  231. if (filePath.startsWith(fileFlag)) {
  232.  
  233. filePath = filePath.substring(fileFlag.length());
  234.  
  235. }
  236.  
  237. final String applicationFlag = "WEB-INF";
  238.  
  239. return filePath.substring(0, filePath.lastIndexOf(applicationFlag));
  240.  
  241. }
  242.  
  243. }
  244.  
  245. }

Java J2EE读取配置文件的更多相关文章

  1. java中读取配置文件ResourceBundle和Properties两种方式比较

    今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网 ...

  2. java web 读取配置文件两种方法

    package com.tsinghua.getDataBaseConn; import java.io.IOException;import java.io.InputStream;import j ...

  3. java后台读取配置文件

    前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家: 先附上代码吧: package com.shafei.util; import java.io. ...

  4. Java中读取配置文件中的内容,并将其赋值给静态变量的方法

    应用场景 项目开发中某个功能需要抽取成方法写成一个工具类,提供给别人使用.写过工具类的人都知道,工具类中的方法一般都是静态方法,可以直接使用类名点方法名调用, 使用很方便,比如判断某个对象是否为空的方 ...

  5. 在java中读取配置文件信息

    public class PropertyUtil { public static final Properties PROP = new Properties(); /** * 读取配置文件的内容( ...

  6. java中读取配置文件的方法

    转自:http://blog.csdn.net/stypace/article/details/38414871 一.使用org.apache.commons.configuration 需要使用的是 ...

  7. 转载:Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解

    唉,读取个文件,也就是在项目里面去获得配置文件的目录,然后,变成文件,有事没事,总是出个 FileNotFoundException  系统找不到指定的文件,气死人啦. 还有就是:System.get ...

  8. java中读取配置文件

    若是Javaweb项目,项目运行于tomcat或其他容器时,可以使用下面方式来获取文件的输入流 1.当属性文件放在src下面时 InputStream is = Thread.currentThrea ...

  9. java中读取配置文件中的数据

    1.先在项目中创建一个包(如:config),再创建一个配置文件(如:a.properties),添加配置信息如下:比如:name=kakaage=28 2.代码:import java.io.IOE ...

随机推荐

  1. mpvue图片轮播遇到的问题

    小程序官方写法: <swiper indicator-dots="{{indicatorDots}}" autoplay="{{autoplay}}" i ...

  2. Liferay7.0与cas单点登录配置

    1.简介     Liferay7.0支持多种登录方式,包括:常规的.opensso.cas.ntlm.ldap.openid.Facebook.Google等. 其中, (1) 常规:则是默认Lif ...

  3. java Queue的用法

    https://www.cnblogs.com/caozengling/p/5307992.html https://blog.csdn.net/a724888/article/details/802 ...

  4. Makefile中.PHONY的作用

    单词phony (即phoney)的意思是:伪造的,假的.来自collins的解释是: If you describe something as phoney, you disapprove of i ...

  5. C++关于vector、queue、stack、priority_queue的元素访问

    vector.queue.stack.priority_queue对元素进行元素访问时,返回的是对应元素的引用.

  6. (win10)Wamp环境下php升级至PHP7.2

    (win10)Wamp环境下php升级至PHP7.2 ①下载php7.2到本地 链接:https://pan.baidu.com/s/16jqmF7GR_CRklHPAZ9VRrg 密码:4ob4 ② ...

  7. GCD之后台程序运行

    点击Home键进入后台时进行计时,直到从新启动,超过三分钟启动手势 // // AppDelegate.m // GCDDown // // Created by City--Online on 15 ...

  8. [日常] Linux下的docker实践

    1.Linux 发展出了另一种虚拟化技术:Linux 容器(Linux Containers,缩写为 LXC) 2.Linux 容器不是模拟一个完整的操作系统,而是对进程进行隔离 3.Docker 属 ...

  9. tomcat日志切割和定期删除(转载)

    tomcat日志切割和定期删除 在tomcat的软件环境中,如果我们任由日志文件无限增长,总有一天会将磁盘占满的(废话).特别是在日志文件增长速度很快的一些情况下,按日志切割日志文件并删除,就是一件很 ...

  10. python基础技巧综合训练题2

    1,判断一个字符串中的每一个字母是否都在另一个字符串中,可以利用集合的特性来解,集合的元素如果存在,再次更新(update) 是添加不进集合的,那么集合的长度还是跟原来一样,如果添加进去,集合长度就会 ...