Java读取Properties文件工具类
2016-04-13 16:05
621 查看
读取properties文件工具类
package zmx.util; import java.io.InputStream; import java.util.Properties; /** * 从类路径下获取资源文件并进行读写 * @author zhangwenchao * */ public class PropertyUtil { private static Properties prop = null; /** * 初始化Properties实例 * @param propertyName * @throws Exception */ public synchronized static void initProperty(String propertyName) throws Exception { if (prop == null) { prop = new Properties(); InputStream inputstream = null; ClassLoader cl = PropertyUtil.class.getClassLoader(); System.out.println(cl); if (cl != null ) { inputstream = cl.getResourceAsStream( propertyName ); } else { inputstream = ClassLoader.getSystemResourceAsStream(propertyName ); } if (inputstream == null) { throw new Exception("inputstream " + propertyName+ " open null"); } prop.load(inputstream); inputstream.close(); inputstream = null; } } /** * 读取数据 * @param propertyName * @param key * @return */ public static String getValueByKey(String propertyName, String key) { String result = ""; try { initProperty(propertyName); result = prop.getProperty(key); return result; } catch (Exception e) { e.printStackTrace(); return ""; } } public static void main(String[] s) { try { System.out.println(PropertyUtil.getValueByKey("config_zh_CN.properties","host")); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- Spring 监听器 IntrospectorCleanupListener简介
- struts1工作流程
- Eclipse使用技巧 - 3. Eclipse选择编译 - 排除编译不想删除的包
- spring mvc中的@propertysource
- [Java] 字符流 Writer,输出字符数据PrintWriter
- java stackoverflowerror与outofmemoryerror区别
- [Java] 字符流Reader,读取字符数据
- java 计算两个日期相差了几个月
- 电子钱包的消费——java card开发第五篇
- JAVA 基本
- Java学习·异常的总结
- NDK在eclipse下的配置及错误解决
- 《UML for Java Programmers》读书笔记——第二章
- Java 自增陷阱
- 学习java的个人经验!
- eclipse运行报Setting property 'source' to 'org.eclipse.jst.jee.server:project' did not find a matching
- 对于jfinal中java.lang.Long cannot be cast to java.lang.Integer的解决方法
- 机器学习:LibSVM与weka在eclipse中的使用
- MyEclipse------制作通讯录
- 学习java要从多方面入手!