标签:java getresourceasstream inputstream properties 读取properties文件
首先,Java中的getResourceAsStream有以下几种:me.class.getResourceAsStream("file/myfile.xml");
源码如下:
package com.wpy.json;
public class ReadPropertiesUtil { public static void readProperties() { Properties prop = new Properties(); //创建Properties对象 //InputStream in = ReadPropertiesUtil.class.getClassLoader().getResourceAsStream("com/wpy/json/data.properties");//加载文件到缓存,从classpath根开始读取 InputStream in = ReadPropertiesUtil.class.getResourceAsStream("data.properties");//读取ReadPropertiesUtil类当前目录的文件,data.properties与ReadPropertiesUtil类在同一个目录 try { prop.load(in); //加载文件 Enumeration enu = prop.propertyNames();//取出所有的key while(enu.hasMoreElements()){ String key = (String) enu.nextElement(); System.out.println("key="+key+",value="+prop.getProperty(key)); } } catch (IOException e) { e.printStackTrace(); } } }
参考自:http://blog.sina.com.cn/s/blog_72c8c1150100pw3r.html
标签:java getresourceasstream inputstream properties 读取properties文件
原文地址:http://blog.csdn.net/wpydaguan/article/details/42458717