这里的配置文件都放在src下面, System.properties的内容

  1. exceptionMapping=exceptionMapping.properties
  2. config=config.properties
  3. sys.core=systemCore.properties
  4. sys.boss=bossPort.properties

bossPort.properties的内容

  1. #查询机场火车站sim卡剩余次数的服务
  2. NgCallServiceURL=http://*.*.*6.*:5***0/esbWS/services/NgCallService
  3. #用户基本信息
  4. sQUserBaseURL=http://*.*.*6.*:5***0/esbWS/services/sQUserBaseL
  5. #用户积分查询
  6. S3979SrcQryURL=http://*.*.*6.*:5***0/esbWS/services/s3979SrcQry

读取properties配置文件的java

  1. import java.io.*;
  2. import java.util.*;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. public class GlobalConfig {
  6.  
  7. private static Log log;
  8. // system.properties是最根的配置文件,并且在src下面
  9. public static final String SYSTEM_PROPERTIES = "/system.properties";
  10.  
  11. private static String propertiesStorePath;
  12. private static Map propertieMap;
  13. private static Map propertieFileMap;
  14.  
  15. static {
  16. log = LogFactory.getLog(com.sinovatech.common.config.GlobalConfig.class);
  17. propertieMap = new HashMap();
  18. propertieFileMap = new HashMap();
  19. Properties properties = init("/system.properties");
  20. Iterator it = properties.keySet().iterator();
  21. propertiesStorePath = properties.getProperty("path");
  22. String name;
  23. Properties p;
  24. for (; it.hasNext(); propertieMap.put(name, p)) {
  25. name = (String) it.next();
  26. String file = properties.getProperty(name);
  27. file = file.trim();
  28. System.out.println();
  29. System.out.println("name "+name+" file "+file);
  30. System.out.println();
  31. propertieFileMap.put(name, file);
  32. p = init("/" + file);
  33. }
  34.  
  35. }
  36.  
  37. public GlobalConfig() {
  38. }
  39.  
  40. private static Properties init(String propertyFile) {
  41. Properties p = new Properties();
  42. try {
  43. log.info("Start Loading property file \t" + propertyFile);
  44. System.out.println("Start Loading property file \t" + propertyFile);
  45. p.load(com.sinovatech.common.config.GlobalConfig.class
  46. .getResourceAsStream(propertyFile));
  47. log.info("Load property file success!\t" + propertyFile);
  48. System.out.println("Load property file success!\t" + propertyFile);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. log.error("Could not load property file." + propertyFile, e);
  52. }
  53. return p;
  54. }
  55.  
  56. public static String getProperty(String cls, String name) {
  57. Properties p = (Properties) propertieMap.get(cls);
  58. if (p != null)
  59. return p.getProperty(name);
  60. else
  61. return null;
  62. }
  63.  
  64. public static boolean getBooleanProperty(String cls, String name) {
  65. String p = getProperty(cls, name);
  66. return "true".equals(p);
  67. }
  68.  
  69. public static Integer getIntegerProperty(String cls, String name) {
  70. String p = getProperty(cls, name);
  71. if (p == null)
  72. return null;
  73. else
  74. return Integer.valueOf(p);
  75. }
  76.  
  77. public static Long getLongProperty(String cls, String name) {
  78. String p = getProperty(cls, name);
  79. if (p == null)
  80. return null;
  81. else
  82. return Long.valueOf(p);
  83. }
  84.  
  85. public static Double getDoubleProperty(String cls, String name) {
  86. String p = getProperty(cls, name);
  87. if (p == null)
  88. return null;
  89. else
  90. return Double.valueOf(p);
  91. }
  92.  
  93. public static void store() {
  94. }
  95.  
  96. public static void store(String cls) {
  97. Properties p = (Properties) propertieMap.get(cls);
  98. try {
  99. FileOutputStream fi = new FileOutputStream(new File(
  100. (String) propertieFileMap.get(cls)));
  101. p.store(fi, "Modified time: " + Calendar.getInstance().getTime());
  102. } catch (FileNotFoundException e) {
  103. e.printStackTrace();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. }

测试GlobalConfig.java读取配置文件信息

  1. public static void main(String[] args) {
  2. String requestUrl=GlobalConfig.getProperty("sys.boss","S3979SrcQryURL");
  3. System.out.println("\n"+requestUrl);
  4. }

测试内容输入:

  1. Start Loading property file /system.properties
  2. Load property file success! /system.properties
  3. name sys.core file systemCore.properties
  4. Start Loading property file /systemCore.properties
  5. Load property file success! /systemCore.properties
  6. name exceptionMapping file exceptionMapping.properties
  7. Start Loading property file /exceptionMapping.properties
  8. Load property file success! /exceptionMapping.properties
  9. name sys.boss file bossPort.properties
  10. Start Loading property file /bossPort.properties
  11. Load property file success! /bossPort.properties
  12. name config file config.properties
  13. Start Loading property file /config.properties
  14. Load property file success! /config.properties
  15. http://*.*.*6.*:5***0/esbWS/services/s3979SrcQry

java项目中读取properties文件的更多相关文章

  1. Java项目中读取properties文件,以及六种获取路径的方法

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

  2. java web中读取properties文件时的路径问题

    在web开发时,难免会有一些固定的参数,我们一般把这些固定的参数存在properties文件中,然后用的时候要读出来.但经常出现一些错误,找不到相应的路径,所以,今天特地讲一些如何正确获得路径. 首先 ...

  3. Java在Web项目中读取properties文件

    import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import ...

  4. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  5. 如何在java类中读取Properties配置文件

    在com.example包下有一个test.properties文件和测试类PropertyReadTest.java. test.properties 文件内容: author=zeige  tea ...

  6. 分别用Java和JS读取Properties文件内容

    项目中经常用到的配置文件,除了XML文件之外,还会用到Properties文件来存储一些信息,例如国际化的设置.jdbc连接信息的配置等.有时候也会把一些路径或者sql语句放到Properties中, ...

  7. Java/JavaWeb中读取资源文件

    1.一般工程中使用I/O类指定文件的绝对路径读取 FileInputStream fis = new FileInputStream("src/main/resources/zsm.prop ...

  8. java代码如何读取properties文件

    我们在开发工程中,有时候需要在Java代码中定义一些在部署生产环境时容易改变的变量,还需要我们单独放在一个外部属性文件中,方便我们将来修改.这里列出了两种比较方便的方式. 一.在Spring配置文件中 ...

  9. 在JavaScript文件中读取properties文件的方法

    假设有JavaScript文件叫做:readproperties.js,这个文件需要读取config.properties这个配置文件,步骤如下: 1.  下载插件jquery.i18n.proper ...

随机推荐

  1. 冒泡排序优化JAVA

    本文对传统的冒泡排序进行了一些优化,减少了循环次数. 时间复杂度 若文件的初始状态是正序的,一趟扫描即可完成排序.所需的关键字比较次数 C 和记录移动次数 M 均达到最小值: C(min)=n-1 , ...

  2. FastDFS基本结构(转)

    0.简介 FastDFS是基于互联网应用的开源分布式文件系统,主要用于大中型网站存储资源文件,如图片.文档.音频.视频等.FastDFS采用类似GFS的架构,用纯C语言实现,支持Linux.FreeB ...

  3. node-restify简介

    restify 是Node.js的模块.虽然restify的API或多或少的参考了express,但restify不是一个MVC框架,它是一套为了能够正确构建REST风格API而诞生的框架. ###安 ...

  4. javascript 中的console.log和弹出窗口alert

    主要是方便你调式javascript用的.你可以看到你在页面中输出的内容. 相比alert他的优点是: 他能看到结构话的东西,如果是alert,淡出一个对象就是[object object],但是co ...

  5. openSUSE 13.1 Milestone 4 发布

    openSUSE 13.1 发布第四个里程碑版本,下载地址: openSUSE-Factory-KDE-Live-Build0652-x86_64.iso (949MB, MD5, torrent) ...

  6. Jpeg2000 简介

    http://www.baike.com/wiki/Jpeg2000 总结Jpeg2000的六个方面:    ⑴ JPEG2000可以方便地实现渐进式传输,这是JPEG2000的重要特征之一.看到这种 ...

  7. SpringMVC关于json、xml自动转换的原理研究[附带源码分析]

    目录 前言 现象 源码分析 实例讲解 关于配置 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.c ...

  8. [外挂4] 用CE查找棋盘基址

    a.找棋盘数据基址 b.分析棋盘数据结构 综合使用搜索技巧,这要看你的聪明才智啦! [如本例:首先精确查找0,然后点一下左上角的一个,再次筛选出变化的,重开盘,再搜变化的,发现期盼规律为值为0表示没有 ...

  9. jQuery Mobile + HTML5

    最近项目需要,需要构建一个适合手持设备访问的站点,作者从网上查阅了一些资料,本文就是基于此而来. 首先下载jQuery Mobile http://jquerymobile.com/,选择稳定版即可. ...

  10. ASP.NET 5系列教程(七)完结篇-解读代码

    在本文中,我们将一起查看TodoController 类代码. [Route] 属性定义了Controller的URL 模板: [Route("api/[controller]") ...