package com.rambo.tools; import java.io.*; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; import java.util.Properties; /** * 通过类加载器,获取资源 */ public final class Resources { private static ClassLoader defaultClassLoader; private static Charset charset; private Resources() { } public static ClassLoader getDefaultClassLoader() { return defaultClassLoader; } public static void setDefaultClassLoader(ClassLoader defaultClassLoader) { Resources.defaultClassLoader = defaultClassLoader; } public static URL getResourceURL(String resource) throws IOException { return getResourceURL(getClassLoader(), resource); } public static URL getResourceURL(ClassLoader loader, String resource) throws IOException { URL url = null; if (loader != null) { url = loader.getResource(resource); } if (url == null) { url = ClassLoader.getSystemResource(resource); } if (url == null) { throw new IOException("Could not find resource " + resource); } return url; } public static InputStream getResourceAsStream(String resource) throws IOException { return getResourceAsStream(getClassLoader(), resource); } public static InputStream getResourceAsStream(ClassLoader loader,String resource) throws IOException { InputStream in = null; if (loader != null) { in = loader.getResourceAsStream(resource); } if (in == null) { in = ClassLoader.getSystemResourceAsStream(resource); } if (in == null) { throw new IOException("Could not find resource " + resource); } return in; } public static Properties getResourceAsProperties(String resource) throws IOException { Properties props = new Properties(); InputStream in = null; String propfile = resource; in = getResourceAsStream(propfile); props.load(in); in.close(); return props; } public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException { Properties props = new Properties(); InputStream in = null; String propfile = resource; in = getResourceAsStream(loader, propfile); props.load(in); in.close(); return props; } public static Reader getResourceAsReader(String resource) throws IOException { return (charset == null) ? new InputStreamReader(getResourceAsStream(resource)) : new InputStreamReader(getResourceAsStream(resource), charset); } public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException { Reader reader; if (charset == null) { reader = new InputStreamReader( getResourceAsStream(loader, resource)); } else { reader = new InputStreamReader( getResourceAsStream(loader, resource), charset); } return reader; } public static File getResourceAsFile(String resource) throws IOException { return new File(getResourceURL(resource).getFile()); } public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException { return new File(getResourceURL(loader, resource).getFile()); } public static InputStream getUrlAsStream(String urlString) throws IOException { URL url = new URL(urlString); URLConnection conn = url.openConnection(); return conn.getInputStream(); } public static Reader getUrlAsReader(String urlString) throws IOException { return new InputStreamReader(getUrlAsStream(urlString)); } public static Properties getUrlAsProperties(String urlString) throws IOException { Properties props = new Properties(); InputStream in = null; String propfile = urlString; in = getUrlAsStream(propfile); props.load(in); in.close(); return props; } public static Class<?> classForName(String className) throws ClassNotFoundException { Class<?> clazz = null; try { clazz = getClassLoader().loadClass(className); } catch (Exception e) { // Ignore. Failsafe below. } if (clazz == null) { clazz = Class.forName(className); } return clazz; } public static Object instantiate(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException { return instantiate(classForName(className)); } public static Object instantiate(Class<?> clazz) throws InstantiationException, IllegalAccessException { return clazz.newInstance(); } private static ClassLoader getClassLoader() { if (defaultClassLoader != null) { return defaultClassLoader; } else { return Thread.currentThread().getContextClassLoader(); } } public static Charset getCharset() { return charset; } public static void setCharset(Charset charset) { Resources.charset = charset; } public static void main(String[] args) { } }