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; ...
随机推荐
- CSS 分组
选择器分组 假设希望 h2 元素和段落都有灰色.为达到这个目的,最容易的做法是使用以下声明: h2, p {color:gray;} 将 h2 和 p 选择器放在规则左边,然后用逗号分隔,就定义了一个 ...
- SQL:查找被锁的表,以及锁表的SQL语句(重点推荐)
--死锁检测 use master Select * --找到SPID exec sp_lock --根据SPID找到OBJID ) --根据OBJID找到表名 1.DatabaseName 同于你要 ...
- 二、Spring——AoP
AOP概述 AOP是OOP的有益补充,他为程序开发提供了一个崭新的思考角度,可以将重复性的横切逻辑抽取到统一的模块中,通过OOP的纵向抽象和AOP的横向抽取,程序才能真正解决复杂性代码问题. Spri ...
- 学习SQL的点点滴滴(一)-常用函数
该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很详细. 以下所有例子均Studnet表为例 ...
- linux 下 文件权限和文件主
文件与文件夹的权限和所有者 1.chmod -R 755 file 777 含义与来源: 777含义:分别为:所有者.同组用户.其他用户 7的来源:文件有三种操作模式:读4.写2.执行1,分别值为42 ...
- Oracle数据库的后备和恢复————关于检查点的一些知识
当我们使用一个数据库时,总希望数据库的内容是可靠的.正确的,但由于计算机系统的故障(硬件故障.软件故障.网络故障.进程故障和系统故障)影响数据库系统的操作,影响数据库中数据的正确性,甚至破坏数据库,使 ...
- IOS的UI总结
一.UIView常见属性 1.frame 位置和尺寸(以父控件的左上角为原点(0,0)) 2.center 中点(以父控件的左上角为原点(0,0)) 3.bounds 位置和尺寸(以自己的左上角为 ...
- Codeforces Round #229 (Div. 2) C
C. Inna and Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input stand ...
- resignFirstResponder
What is this resignFirstResponder business? Here is the short version:Some view objects are also con ...
- Ubuntu 12.10使用apt安装Oracle/Sun JDK
apt-get install python-software-properties sudo add-apt-repository ppa:webupd8team/java sudo apt-get ...