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

exceptionMapping=exceptionMapping.properties
config=config.properties
sys.core=systemCore.properties
sys.boss=bossPort.properties

bossPort.properties的内容

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

读取properties配置文件的java

import java.io.*;
import java.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class GlobalConfig { private static Log log;
// system.properties是最根的配置文件,并且在src下面
public static final String SYSTEM_PROPERTIES = "/system.properties"; private static String propertiesStorePath;
private static Map propertieMap;
private static Map propertieFileMap; static {
log = LogFactory.getLog(com.sinovatech.common.config.GlobalConfig.class);
propertieMap = new HashMap();
propertieFileMap = new HashMap();
Properties properties = init("/system.properties");
Iterator it = properties.keySet().iterator();
propertiesStorePath = properties.getProperty("path");
String name;
Properties p;
for (; it.hasNext(); propertieMap.put(name, p)) {
name = (String) it.next();
String file = properties.getProperty(name);
file = file.trim();
System.out.println();
System.out.println("name "+name+" file "+file);
System.out.println();
propertieFileMap.put(name, file);
p = init("/" + file);
} } public GlobalConfig() {
} private static Properties init(String propertyFile) {
Properties p = new Properties();
try {
log.info("Start Loading property file \t" + propertyFile);
System.out.println("Start Loading property file \t" + propertyFile);
p.load(com.sinovatech.common.config.GlobalConfig.class
.getResourceAsStream(propertyFile));
log.info("Load property file success!\t" + propertyFile);
System.out.println("Load property file success!\t" + propertyFile);
} catch (Exception e) {
e.printStackTrace();
log.error("Could not load property file." + propertyFile, e);
}
return p;
} public static String getProperty(String cls, String name) {
Properties p = (Properties) propertieMap.get(cls);
if (p != null)
return p.getProperty(name);
else
return null;
} public static boolean getBooleanProperty(String cls, String name) {
String p = getProperty(cls, name);
return "true".equals(p);
} public static Integer getIntegerProperty(String cls, String name) {
String p = getProperty(cls, name);
if (p == null)
return null;
else
return Integer.valueOf(p);
} public static Long getLongProperty(String cls, String name) {
String p = getProperty(cls, name);
if (p == null)
return null;
else
return Long.valueOf(p);
} public static Double getDoubleProperty(String cls, String name) {
String p = getProperty(cls, name);
if (p == null)
return null;
else
return Double.valueOf(p);
} public static void store() {
} public static void store(String cls) {
Properties p = (Properties) propertieMap.get(cls);
try {
FileOutputStream fi = new FileOutputStream(new File(
(String) propertieFileMap.get(cls)));
p.store(fi, "Modified time: " + Calendar.getInstance().getTime());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }

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

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

测试内容输入:

Start Loading property file     /system.properties
Load property file success! /system.properties
name sys.core file systemCore.properties
Start Loading property file /systemCore.properties
Load property file success! /systemCore.properties
name exceptionMapping file exceptionMapping.properties
Start Loading property file /exceptionMapping.properties
Load property file success! /exceptionMapping.properties
name sys.boss file bossPort.properties
Start Loading property file /bossPort.properties
Load property file success! /bossPort.properties
name config file config.properties
Start Loading property file /config.properties
Load property file success! /config.properties
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. git的一些指令

    1.这是一篇git.github相关具体操作的连接 http://www.cnblogs.com/fanyong/p/3424501.html 2. git remote -v 查看远程分支 git ...

  2. Eclipse中.setting目录下文件介绍

    Eclipse项目中系统文件介绍 一. 写在前面 文章较长,可以直接到感兴趣的段落,或者直接关键字搜索: 请原谅作者掌握的编程语言少,这里只研究Java相关的项目: 每一个文件仅仅做一个常见内容的简单 ...

  3. 跟服务器交互的Web表单(form)

    使用HTML来构建可以跟服务器交互的Web表单(form),通过给你的form元素添加一个action属性来达到此目的. action属性的值指定了表单提交到服务器的地址. 例如: <form ...

  4. python基础整理笔记(一)

    一. 编码 1. 在python2里,加载py文件会对字符进行编码,需要在文件头上的注释里注明编码类型(不加则默认是ascII). # -*- coding: utf-8 -*- print 'hel ...

  5. PHP表单处理

    <?php if(isset($_POST['submit'])) { foreach ($_POST["languages"] as $item) { echo " ...

  6. elixir 高可用系列(一) Agent

    概述 elixir 本身是一种 immutable 的语言,默认情况下,进程间是不共享任何状态的,进程之间通过消息来交互. 而 Agent 则封装了一种进程间共享状态的方式,通过这种方式,不用显式的写 ...

  7. 记一次SQLServer数据库误删数据找回

            昨天 同事在本机清理数据库表时,连接到了生产机,误删了二十几张表,幸好是晚上加班的时候删除的,生产机上当时是一天一备份,还原备份是最后的策略,最关键的还是要找回数据.         ...

  8. ajax异步请求Response.Redirect重定向

    一个ajax异步请求报错->捕获异常->重定向错误提示页面.  一个简单的流程 结果一直搞不定.重定向无效.各种百度之. 后来突然想起 ajax的请求是不能在后台重定向的. 如果硬要重定向 ...

  9. 微软BI 系列随笔列表 (SSIS, SSRS, SSAS, MDX, SQL Server)

    [公告]本博客于2015年10月起不再更新 新博客文章主要发表在商业智能BI社区: http://www.flybi.net/blog/biwork 博客地图自动分类 文章目录方便更好的导航,阅读文章 ...

  10. 在Visio2010中修改默认字体的大小

    由于我常需要在Visio2010中画流程图和UML图,但是Visio2010中的字体默认8px,这对眼睛是个挑战.摸索了好久终于找到在visio2010中修改字体大小的方式. 1.点一下红色箭头所指的 ...