您的位置:首页 > 其它

读取配置文件信息

2015-09-29 12:30 363 查看
java 读取配置文件内容

重点内容

本文由其他博文验证后整理

源地址:http://blog.sina.com.cn/s/blog_7b62c61c0100s8tr.html

读取配置文件相关代码如下:

1.工程下建立一个xx.properties文件

例如:



**想要获取配置文件中的信息下面看取值代码:**


“`

import java.util.Properties;

import java.io.IOException;

import java.io.InputStream;

public class dealSysConfig {

static private String systomPath = null; //配置文件中的字段

static private String commonData = null;//配置文件中的字段

static private String titleNames = null;//配置文件中的字段

static {

loads();

}

synchronized static public void loads() {
if (systomPath == null || commonData == null) {
InputStream is = dealSysConfig.class.getResourceAsStream("/sysConfig.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
systomPath = dbProps.getProperty("systomPath");
commonData = dbProps.getProperty("commonData");
titleNames = dbProps.getProperty("titleNames");
} catch (Exception e) {
System.err.println("不能读取属性文件. "
+ "请确保db.properties在CLASSPATH指定的路径中");
}
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


//————get方法—————-//

public static String getSystomPath() {

if (systomPath == null)

loads();

return systomPath;

}

public static String getCommonData() {
if (commonData == null)
loads();
return commonData;
}

public static String getTitleNames() {
if (titleNames == null)
loads();
return titleNames;
}


//测试是否能够取到值

public static void main(String[] args) {

System.out.println(dealSysConfig.getCommonData());

System.out.println(dealSysConfig.getSystomPath());

System.out.println(dealSysConfig.getTitleNames());

}

输出结果:

7

D:image

嘻嘻嘻(文中翻译后不是这些字,随手为之)

}“`

在此谢谢原博主的博文帮我解决了取值问题,留此文给自己一个备忘。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: