您的位置:首页 > 运维架构

老师编写的读取propertites文件的类

2009-05-23 15:39 106 查看
package cn.qdqn.tool;
import java.io.IOException;
import java.util.Properties;
public class MyConfigTool {
private static Properties prop;
public synchronized static String getProperty(String key) {
if (null == prop) {
prop = new Properties();
try {
prop.load(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("config/config.properties"));
} catch (IOException e) {
e.printStackTrace();
System.out.println("config/config.properties 加载失败 !");
}
}
return prop.getProperty(key);
}
}


其中,老师使用了Thread.currentThread().getContextClassLoader(),此处是获得当前线程的上下文类加载器。

再通过类加载器的getResourceAsStream()方法读取了properties文件。

这样大大的减少了代码量,而且,文件路径就相当于从app的根目录开始,不容易出错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  properties string class null
相关文章推荐