1. /*
  2. * Copyright (c) 2017. Panteng.Co.Ltd All rights reserved
  3. */
  4. import org.apache.log4j.Logger;
  5.  
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.Properties;
  10.  
  11. /**
  12. * @author panteng
  13. * @description
  14. * @date 17-2-7.
  15. */
  16. public class PropertiesUtil {
  17. public static Logger logger = Logger.getLogger(PropertiesUtil.class);
  18. public static Properties pros = new Properties();
  19.  
  20. static {
  21. // 获取配置文件所在文件夹
  22. String configDir = PropertiesUtil.class.getClassLoader().getResource("").getPath();
  23. // 遍历文件夹下面的所有配置文件
  24. File dir = new File(configDir);
  25. File[] files = dir.listFiles();
  26. for (int i = 0; i < files.length; i++) {
  27. if (files[i].getName().indexOf(".properties") > -1) {
  28. InputStream path = PropertiesUtil.class.getClassLoader().getResourceAsStream(files[i].getName());
  29. try {
  30. pros.load(path);
  31. } catch (IOException e) {
  32. logger.error("{}", e);
  33. }
  34. }
  35. }
  36. }
  37. }

对于打包成的jar包文件,需要读取jar里面的配置文件时,就会出现问题!对应修改如下:

  1. /*
  2. * Copyright (c) 2017. Xiaomi.Co.Ltd All rights reserved
  3. */
  4.  
  5. package com.xiaomi.weather.utils;
  6.  
  7. import org.apache.log4j.Logger;
  8.  
  9. import java.io.*;
  10. import java.util.Properties;
  11.  
  12. /**
  13. * @author panteng
  14. * @description
  15. * @date 17-2-7.
  16. */
  17. public class PropertiesUtil {
  18. public static Logger logger = Logger.getLogger(PropertiesUtil.class);
  19. public static Properties pros = new Properties();
  20.  
  21. static {
  22. // 获取配置文件所在文件夹
  23. String configDir = PropertiesUtil.class.getClassLoader().getResource("").getPath();
  24. if (configDir.indexOf(".jar!") > -1) {//jar包
  25. try {
  26. InputStream ips = PropertiesUtil.class.getResourceAsStream("/service.properties");
  27. BufferedReader ipss = new BufferedReader(new InputStreamReader(ips));
  28. pros.load(ipss);
  29. System.out.println("============================XXX" + pros.get("mongoHost"));
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. } else {
  34. // 遍历文件夹下面的所有配置文件
  35. File dir = new File(configDir);
  36. File[] files = dir.listFiles();
  37. for (int i = 0; i < files.length; i++) {
  38. if (files[i].getName().indexOf(".properties") > -1) {
  39. InputStream path = PropertiesUtil.class.getClassLoader().getResourceAsStream(files[i].getName());
  40. try {
  41. pros.load(path);
  42. } catch (IOException e) {
  43. logger.error("{}", e);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }

获取jar内配置文件(scala):

  1. val regularInputStream = this.getClass.getClassLoader.getResourceAsStream("regular.txt")
    val regularBr = new BufferedReader(new InputStreamReader(regularInputStream))
    var line: String = null
    while ( {
    line = regularBr.readLine();
    line != null
    }) {
    val i1 = line.indexOf("\t")
    val i2 = line.indexOf("\t", i1 + 1)
    val i3 = line.indexOf("\t", i2 + 1)
    val i4 = line.indexOf("\t", i3 + 1)
    val i5 = line.indexOf("\t", i4 + 1)
    descriptions.append(new Regex(line.substring(i4 + 1, i5)))
    appIds.append(line.substring(i1 + 1, i2))
    titles.append(null)
    }

Java properties配置文件工具类的更多相关文章

  1. Java读取properties配置文件工具类

    1.   PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...

  2. Java加载Properties配置文件工具类

    Java加载Properties配置文件工具类 import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; ...

  3. 开发读取.properties 配置文件工具类PropertiesUtil

    import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.juni ...

  4. Java基础-DButils工具类(QueryRunner)详解

    Java基础-DButils工具类(QueryRunner)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC ...

  5. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  6. Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类

    Java 敏感词过滤,Java 敏感词替换,Java 敏感词工具类   =========================== ©Copyright 蕃薯耀 2017年9月25日 http://www ...

  7. java 邮件发送工具类【来源网络自己已经实际应用】

    最近在做一个Java发送邮件的工具类,现在分享一下完整的代码 首先需要java邮件的包javax.mail-1.5.4.jar 之前因为链接给错了,很不好意思,现在重新发一次. 包在这里可以下载htt ...

  8. HttpTool.java(在java tool util工具类中已存在) 暂保留

    HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...

  9. java文件处理工具类

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...

随机推荐

  1. 解决ios8 webView加载的地图无法定位问题

    本文转载至http://www.cocoachina.com/bbs/read.php?tid-237825.html     1.在文件info.pilist 中导入 NSLocationWhenI ...

  2. Android开发:《Gradle Recipes for Android》阅读笔记1.5

    这节讲的是如何如何添加JAVA依赖库. 默认的android项目有两个build.gradle文件,分别位于顶级目录,和应用自己的目录下(通常放在一个叫app的目录下面). gradle支持多种方式列 ...

  3. 构造三层时报错“程序 “D:\MyTest\....”不包含适合于入口点的静态"Main"方法”

    错误 1 程序“D:\MyTest\EBookShop\Model\obj\x86\Debug\Model.exe”不包含适合于入口点的静态“Main”方法 原因:原来创建项目的时候,用的是“空项目” ...

  4. Android打印日志管理

    做项目的时候,免不了要打印许多日志,等项目上线了,想要去除日志是又找不到在哪里怎么办?我们可以建立一个日志打印的类来统一管理: public class LogUtil { public static ...

  5. EasyNVR智能云终端硬件使用说明(EasyNVR无插件直播服务硬件的具体使用方法)

    问题背景 随着EasyNVR硬件版本(EasyNVR硬件云终端)的发布不少客户选择了EasyNVR云终端作为产品选择,在客户收到EasyNVR云终端的时候肯定都有一个疑问,那就是如何使用手头上的这个小 ...

  6. 基于EasyNVR摄像机无插件直播流媒体服务器实现类似于单点登录功能的免登录直播功能

    提出问题 EasyNVR是一套摄像机无插件直播的流媒体服务器软件,他可以接入各种各样的摄像机,再经过转化统一输出无插件化直播的RTMP.HLS.HTTP-FLV流,同时,EasyNVR为了数据安全,提 ...

  7. 前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "

    前台传递给后台的JSON字符串中的引号 "" 在JAVA后台被转义为 &quot 1.问题: 前台数据,JSON字符串带有引号 "" ,数据被传递到后台 ...

  8. DropdownList异步刷新GridView数据

    前台代码: <div style=" clear:both; width:800px; text-align:center; margin-left:auto; margin-righ ...

  9. js引入方式的弹框方法2

    html代码: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv=& ...

  10. _ 下划线 Underscores __init__

    Underscores in Python https://shahriar.svbtle.com/underscores-in-python Underscores in Python This p ...