Java 读取类路径下的资源文件实现代码
Java读取类路径下的资源文件实现代码
一、工具类代码ResourceLoadUtil.java
importjava.io.IOException; importjava.io.InputStream; importjava.net.URL; importjava.util.ArrayList; importjava.util.Enumeration; importjava.util.List; publicclassResourceLoadUtil{ /** *@paramargs */ publicstaticvoidmain(String[]args){ System.out.println("ByClazz...\n--------------------------"); System.out.println(loadResourceByClazz(String.class,"/java/lang/String.class")); System.out.println(loadResourceByClazz(String.class,"/ResourceLoadUtil.class")); System.out.println(); System.out.println("ByClasLoader...\n--------------------------"); System.out.println(loadResourceByClassLoader("java/lang/String.class")); System.out.println(loadResourceByClassLoader("ResourceLoadUtil.class")); System.out.println(); System.out.println("sByClasLoader...\n--------------------------"); System.out.println(loadResourcesByClassLoader("java/lang/String.class")); System.out.println(loadResourcesByClassLoader("ResourceLoadUtil.class")); System.out.println(loadResourcesByClassLoader("struts-plugin.xml")); } /** *根据Class加载资源文件 * *@paramclazz *@parampath *以'/'开头的路径:使用绝对路径寻找文件(以ClassPath为根路径开始)
*否则:使用相对路径寻找文件(相对于clazz位置路径开始) *@return */ publicstaticInputStreamloadResourceByClazz(Class>clazz,Stringpath){ returnclazz.getResourceAsStream(path); } /** *根据ClassLoader加载资源文件(单个),使用默认的ClassLoader! * *@parampath *不能以'/'开头的路径 *@return */ publicstaticInputStreamloadResourceByClassLoader(Stringpath){ returnloadResourceByClassLoader(null,path); } /** *根据ClassLoader加载资源文件(单个),使用默认的ClassLoader! * *@parampath *不能以'/'开头的路径 *@return */ publicstaticInputStreamloadResourceByClassLoader(ClassLoadercl,Stringpath){ if(cl==null){ cl=Thread.currentThread().getContextClassLoader(); } returncl.getResourceAsStream(path); } /** *根据ClassLoader加载资源文件(多个),使用默认的ClassLoader! * *@parampath *不能以'/'开头的路径 *@return */ publicstaticListloadResourcesByClassLoader(Stringpath){ returnloadResourcesByClassLoader(null,path); } /** *根据ClassLoader加载资源文件(多个),使用指定的ClassLoader! * *@parampath *不能以'/'开头的路径 *@return */ publicstaticList loadResourcesByClassLoader(ClassLoadercl,Stringpath){ if(cl==null){ cl=Thread.currentThread().getContextClassLoader(); } List urlLst=newArrayList (); try{ Enumeration urlsEnum=cl.getResources(path); while(urlsEnum.hasMoreElements()){ URLurl=(URL)urlsEnum.nextElement(); urlLst.add(url); } }catch(IOExceptione){ e.printStackTrace(); } returnurlLst; } }
二、执行结果
ByClazz... -------------------------- sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@11cfb549 java.io.BufferedInputStream@a422ede ByClasLoader... -------------------------- sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@7f12f614 java.io.BufferedInputStream@35d9dc39 sByClasLoader... -------------------------- [jar:file:/D:/Java/jdk1.6.0_25/jre/lib/rt.jar!/java/lang/String.class] [file:/E:/workspace/JBDS/Spring+Struts2-Integration/target/classes/ResourceLoadUtil.class] [jar:file:/E:/360data/%e9%87%8d%e8%a6%81%e6%95%b0%e6%8d%ae/.m2/repository/org/apache/struts/struts2-spring-plugin/2.3.4.1/struts2-spring-plugin-2.3.4.1.jar!/struts-plugin.xml,jar:file:/E:/360data/%e9%87%8d%e8%a6%81%e6%95%b0%e6%8d%ae/.m2/repository/org/apache/struts/struts2-config-browser-plugin/2.3.4.1/struts2-config-browser-plugin-2.3.4.1.jar!/struts-plugin.xml]
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!