📄 resourceloader.java
字号:
package com.lyrisoft.util.io;import java.io.InputStream;import java.util.StringTokenizer;/** * A class that loads resources from locations relative to the CLASSPATH * * $Id: ResourceLoader.java,v 1.3 2000/10/04 07:13:46 taso Exp $ */public class ResourceLoader { /** * Get a resource as an InputStream from within the CLASSPATH. * * Example: If I am an instance of foo.bar.AnObject, and I call * <pre> * InputStream is = ResourceLoader.getResource(this, myfile.txt); * </pre> * * I will get an InputStream to the file, "myfile.txt", from the first * occurence of foo/bar/myfile.txt, relative to the classpath. * * @param caller the caller object - * used to determine the package (directory) * @param filename the name of the resource * @exception ResourceException if the resource is not found */ public static InputStream getResource(Object caller, String filename) throws ResourceException { Class c = null; if (caller instanceof Class) { c = (Class)caller; } else { c = caller.getClass(); } // resolve pathname from the classname // (change dots to slashes and append the filename) StringTokenizer st = new StringTokenizer(c.getName(), "."); StringBuffer sb = new StringBuffer(); int cnt = st.countTokens(); for (int i=0; i < cnt-1; i++) { if (i > 0) { sb.append("/"); } sb.append(st.nextToken()); } sb.append("/"); sb.append(filename); String pathname = sb.toString(); return getResource(pathname); } /** * Load a resource from the CLASSPATH. relativePath should use forward-slashes * and omit the first slash. * * e.g., getResource("path/to/my/resource"); * * @param relativePath */ public static InputStream getResource(String relativePath) throws ResourceException { // we use the actual ClassLoader rather than the simple, // Class.getResource() method, because we want the SYSTEM // ClassLoader, not a Servlet ClassLoader, or any other loader. InputStream is = ClassLoader.getSystemResourceAsStream(relativePath); if (is == null) { throw new ResourceException("Could not locate resource, " + relativePath); } return is; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -