老师编写的读取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的根目录开始,不容易出错
相关文章推荐
- 链表 编写链表将数字倒序输出的C语言程序。(我们C语言老师讲的!)
- 小娜老师的讲义-Docker基础知识-编写Dockerfile
- 感谢郁金香老师把我带入编写辅助的大门
- VS2010编写UDP网络通信程序的问题(借用孙鑫老师《深入VC++》书中程序)
- Linux基础之linux的历史(针对韩立刚老师的linux教学视频编写的笔记)
- python - 一个学校,有3个办公室,现在有8位老师等待工位的分配,请编写程序,完成随机的分配.
- 舒冰冰老师为某通信公司编写电话营销中心管理部分内容
- Data.cpp老师的作业,叫咱编写一个存放日期的data类
- 感谢郁金香老师把我带入编写辅助的大门
- 7-2 一帮一(10 分) “一帮一学习小组”是中小学中常见的学习组织方式,老师把学习成绩靠前的学生跟学习成绩靠后的学生排在一组。本题就请你编写程序帮助老师自动完成这个分配工作,即在得到全班学生的排名
- 黑马day06 张孝祥老师编写的EL函数标签库
- 老师布置的C++实践作业:C++编写的学生系统
- JAVA私塾的基础入门教材完全是老师自已编写
- 黑马程序员 JDBC_编写一个基本的连接池来实现连接的复用,优化工作(李勇老师)
- 廖雪峰老师项目实践Day4---编写model
- 【VB.NET】打造一个象棋魔法学校的老师——谨以此文献给象棋爱好者——编写界面和功能的最终实现
- Linux基础之linux终端和shell命令(针对韩立刚老师的linux教学视频编写的笔记)
- PostgreSQL数据库动态函数脚本编写注意事项
- 老师 感觉是四川人讲的挺好的
- python编写ttyshell的反向链接后门