标签:style io ar 使用 java for sp 文件 on
为了修改项目参数方便,需要使用properties配置文件;
首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包)
commons comfigurations
依赖包
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);
}
}
标签:style io ar 使用 java for sp 文件 on
原文地址:http://www.cnblogs.com/castlehappiness/p/4034905.html