由于Java中读取配置文件的代码比较固定,所以可以将读取配置文件的那部分功能单独作为一个类,以后可以复用。为了能够达到复用的目的,不能由配置文件中每一个属性生成一个函数去读取,我们需要一种通用的方法读取属性,即由用户给出属性名字(作为方法参数)来获取对应属性的Value值。下面是示例代码:

 import java.io.*;
import java.util.*; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public class Configure { // private static final Log log = LogFactory.getLog(ServerConfig.class);
private static Properties config = null; public Configure() {
config = new Properties();
} public Configure(String filePath) {
config = new Properties();
try {
ClassLoader CL = this.getClass().getClassLoader();
InputStream in;
if (CL != null) {
in = CL.getResourceAsStream(filePath);
}else {
in = ClassLoader.getSystemResourceAsStream(filePath);
}
config.load(in);
// in.close();
} catch (FileNotFoundException e) {
// log.error("服务器配置文件没有找到");
System.out.println("服务器配置文件没有找到");
} catch (Exception e) {
// log.error("服务器配置信息读取错误");
System.out.println("服务器配置信息读取错误");
}
} public String getValue(String key) {
if (config.containsKey(key)) {
String value = config.getProperty(key);
return value;
}else {
return "";
}
} public int getValueInt(String key) {
String value = getValue(key);
int valueInt = 0;
try {
valueInt = Integer.parseInt(value);
} catch (NumberFormatException e) {
e.printStackTrace();
return valueInt;
}
return valueInt;
}
}

单元测试:

    @Test
public void configureTest() {
Configure config = new Configure("server.properties");
int port = config.getValueInt("server.port");
String ip = config.getValue("server.ip");
String sp = config.getValue("message.split");
System.out.println("port: " + port);
System.out.println("ip: " + ip);
System.out.println("sp: " + sp);
}

配置文件如下:

server.port =30000
server.ip=127.0.0.1
server.backgroundRun = false
MAX_ERROR_NUM=1000
message.split=\#
message.over=31
message.serverGetMessage=Yes
message.wrong=No
message.serverGetOver=over
message.serverFindSIM=find
message.serverNotFindSIM=NotFind

Java中读取.properties配置文件的通用类的更多相关文章

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

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

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

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

  3. Java中读取 .properties 和 .xml 文件

    配置文件内容获取 总结内容 1. Java中为什么要使用配置文件 2. Java中常用的配置文件类型有哪些以及它们的特点 Properties配置文件 XML配置文件 总结 总结内容 1. Java中 ...

  4. Java中读取properties资源文件

    一.通过ResourceBundle来读取.properties文件 /** * 通过java.util.resourceBundle来解析properties文件. * @param String ...

  5. java 如何读取 properties 配置文件

  6. javaweb 读取properties配置文件参数

    场景1:在servlet中读取properties配置文件参数 protected void doGet(HttpServletRequest request, HttpServletResponse ...

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

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

  8. java读取properties配置文件总结

    java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...

  9. java读取properties配置文件信息

    一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...

随机推荐

  1. 前端开发-3-HTML-body标签

    body标签 h.p.a.ul.ol.div.img. 想要在网页上展示出来的内容一定要放在body标签中. 把我们之前海燕那一段HTML代码贴过来,保存到一个HTML格式的文件中. <!DOC ...

  2. IExpress 制作安装包 注意事项

    被打包的文件名不能超过8个字符,否则iexpress会取前6个字符 + "~1".

  3. AMD 与CMD

    AMD AMD是"Asynchronous Module Definition"的缩写,意思就是"异步模块定义".它采用异步方式加载模块,模块的加载不影响它后面 ...

  4. cacti客户端snmp设置

    1. ubuntu : apt-get install snmp snmpd vim /etc/default/snmpd  //将此配置文件中127.0.0.1 删掉. /etc/init.d/sn ...

  5. TEXT 8 Ready, fire, aim

    TEXT 8 Ready, fire, aim 预备!开火!瞄准!! Feb 16th 2006 From The Economist print edition Foreword:A vice-pr ...

  6. 基元线程同步构造之信号量(Semaphore)

    信号量(semaphore)不过是由内核维护的 int32变量而已,(说通俗点就是好比一个线程容器里面允许执行的线程数,0计数就是允许执行的0个线程数,1就是允许执行的1个线程数,2就是允许执行的2个 ...

  7. IDEA artifacts Web Application:Exploded Web Application:Archive

    首先,artifacts是maven中的一个概念,表示项目/modules如何打包,比如jar,war,war exploded,ear等打包形式,一个项目或者说module有了artifacts 就 ...

  8. IE6 PNG不透明问题 (只解决img标签的图片)

    <script type='text/javascript' src="/script/ie6.pngfix.js"></script> 会让一些posit ...

  9. css3文字截断

    width:200px; height:14px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; text-overflow ...

  10. cf451C-Predict Outcome of the Game

    http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS     ...