java.util.Properties 配置文件
2012-04-25 15:33
337 查看
首先通过下面的类获取Properties:
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
//import org.apache.commons.logging.LogFactory;
public class Configuration {
private static Properties props = null;
private static InputStream input = null;
public static String getProperty(String key){
if(props==null)
initProperties();
return props.getProperty(key);
}
public static void initProperties() {
props = new Properties();
try {
if (props.size() == 0) {
input = Configuration.class.getClassLoader()
.getResourceAsStream("application.properties");
props.load(input);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
input = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
然后:通过 Configuration.getProperty("svc.url");获取参数内容
import com.common.util.Configuration;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Configuration.getProperty("svc.url"));
}
}
application.properties内容格式:
svc.url=http://10.109.17.20:8888/svcBiz-webservice/ForSVCService
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
//import org.apache.commons.logging.LogFactory;
public class Configuration {
private static Properties props = null;
private static InputStream input = null;
public static String getProperty(String key){
if(props==null)
initProperties();
return props.getProperty(key);
}
public static void initProperties() {
props = new Properties();
try {
if (props.size() == 0) {
input = Configuration.class.getClassLoader()
.getResourceAsStream("application.properties");
props.load(input);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (input != null) {
input.close();
input = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
然后:通过 Configuration.getProperty("svc.url");获取参数内容
import com.common.util.Configuration;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Configuration.getProperty("svc.url"));
}
}
application.properties内容格式:
svc.url=http://10.109.17.20:8888/svcBiz-webservice/ForSVCService
相关文章推荐
- Java.util.logging日志入门教程(二)properties配置(输出文件)
- Eclipse和Tomcat和Java读取配置文件工具类PropertiesUtil
- java.util.ResourceBundle 和java.util.properties 读取配置文件区别
- java.util.properties工具类操作properties配置文件
- java.util.ResourceBundle 和java.util.properties 读取配置文件区别
- java读取配置文件(使用java.util.Properties读取)
- java.util.Properties读取中文内容的配置文件,发生中文乱码的现象有解决方案
- 使用java.util.Properties读取配置文件信息
- java获取当前路径及加载配置文件(报错:java.lang.NullPointerException java.util.Properties$LineReader.readLine(Prop)
- Java Properties 类读取配置文件信息
- 扫描配置文件(二)Java 读写Properties配置文件
- java 顺序 读写 Properties 配置文件
- java读取配置文件( properties 与 xml )
- Java读取properties配置文件时,中文乱码解决方法
- java 加载properties配置文件属性到常量
- java工具类开发之日期工具类DateUtils和读取属性配置文件工具类PropertiesUtils
- 使用java.util.Properties读取文件
- Java Properties 类读取配置文件信息
- Java代码中获取配置文件(config.properties)中内容的两种方法
- java 数据库读取工具类(读取config.properties配置文件)