java读取properties配置文件方法(一)
为了修改项目参数方便,需要使用properties配置文件;
首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包)
commons configurations
依赖包
commons-lang
commons-logging
然后开始就可以编写读取文件的代码了
现在先简单说一下第一种,如有不足之处欢迎指教。
package properties;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class PropertiesConfig{
/**
* read ip and port from the file with PATH:ustc/com/source/global.properties
*
*/
private static String fileName = "ustc/com/source/global.properties";
/**
* @return port
* @throws ConfigurationException
*/
public static Integer InitPort() throws ConfigurationException {
Configuration config = new PropertiesConfiguration(fileName);
Integer port = config.getInt("port");
return port;
}
/**
*
* @return ip
* @throws ConfigurationException
*/
public static String InitIP() throws ConfigurationException {
Configuration config = new PropertiesConfiguration(fileName);
String IP = config.getString("IP");
return IP;
}
/**
*
* for test
*/
public static void main(String argc[]) {
Integer port = null;
String ip = null;
try {
port = PropertiesConfig.InitPort();
ip = PropertiesConfig.InitIP();
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(port + ip);
}
}
java读取properties配置文件方法(一)的更多相关文章
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...
- 【转载】java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...
- java读取properties配置文件信息
一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...
- Java 读取 .properties 配置文件的几种方式
Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配 ...
- Java 读取 .properties 配置文件
java 开发中,经常要读取 properties 配置文件,下面介绍几种读取方式: 1.基于 InputStream 读取配置文件 该方式的优点在于可以读取任意路径下的配置文件 Properties ...
- Java读取Properties配置文件
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,使用键值对的形式来保存属性集.不过Properties的键和值都是字符串 ...
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- java读取properties配置文件的方法
app.properties mail.smtp.host=smtp.163.com mail.transport.protocol=smtp import java.io.InputStream; ...
随机推荐
- Property ClientHeight does not exist 问题解决
delphi的TFrame继承自另一个TFrame时,最好通过File->New->Other...->Delphi Projects->Inheritable Items 的 ...
- CSS3 旋转代码备忘
.Aclose { -webkit-transition-property: all; -webkit-transition-duration: .3s; -moz-transition-proper ...
- urlrewriter的使用
开源类库地址 https://github.com/sethyates/urlrewriter/find/master <?xml version="1.0"?> &l ...
- 关于CGContextSetBlendMode: invalid context 0x0的错误
在ios 7的模拟器中,选择一个输入框准备输入时,会触发这个错误,以下是出错详细日志: <Error>: CGContextSetBlendMode: invalid context 0x ...
- sql server 相似度对比
转自:http://www.dotblogs.com.tw/rachen/archive/2008/10/07/5611.aspx 函數一.產生 Like 比對用字串 ) ) ) as begin / ...
- [Tex学习笔记]积分平均
$\def\avint{\mathop{\mathchoice{\,\rlap{-}\!\!\int} {\rlap{\raise.15em{\scriptstyle -}}\kern-.2em\in ...
- 中大bbs
发现个好像很好玩的东东, 中大bbs: luit -encoding gbk telnet bbs.sysu.edu.cn
- JavaWEB监听器
1.基本概念 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext, HttpSession和 ServletRequest等域对象 ...
- cocoapod 安装
淘宝镜像: sudo gem sources -a https://ruby.taobao.org/ sudo gem sources --remove https://rubygems.org/ 安 ...
- JavaScript基础篇
写的不错,转 http://www.cnblogs.com/suoning/p/5656403.html