Java之资源文件读取

ClassLoaderWrapper.java
package org.utils.resource;

import java.io.InputStream;
import java.net.URL;

class ClassLoaderWrapper {
	
	ClassLoader defaultClassLoader;
	ClassLoader systemClassLoader;
	
	ClassLoaderWrapper() {
		try {
			systemClassLoader = ClassLoader.getSystemClassLoader();
		}
		catch (SecurityException ignored) {}
	}
	
	public URL getResourceAsURL(String resource) {
		return getResourceAsURL(resource, getClassLoaders(null));
	}
	
	public URL getResourceAsURL(String resource, ClassLoader classLoader) {
		return getResourceAsURL(resource, getClassLoaders(classLoader));
	}
	
	public InputStream getResourceAsStream(String resource) {
		return getResourceAsStream(resource, getClassLoaders(null));
	}
	
	public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {
		return getResourceAsStream(resource, getClassLoaders(classLoader));
	}
	
	public Class<?> classForName(String name) throws ClassNotFoundException {
		return classForName(name, getClassLoaders(null));
	}
	
	public Class<?> classForName(String name, ClassLoader classLoader) throws ClassNotFoundException {
		return classForName(name, getClassLoaders(classLoader));
	}
	
	InputStream getResourceAsStream(String resource, ClassLoader[] classLoaderArray) {
		for (ClassLoader classLoader : classLoaderArray) {
			if (null != classLoader) {
				InputStream returnValue = classLoader.getResourceAsStream(resource);
				if (null == returnValue) returnValue = classLoader.getResourceAsStream("/" + resource);
				if (null != returnValue) return returnValue;
			}
		}
		
		return null;
	}
	
	URL getResourceAsURL(String resource, ClassLoader[] classLoaderArray) {
		URL url;
		
		for (ClassLoader classLoader : classLoaderArray) {
			if (null != classLoader) {
				url = classLoader.getResource(resource);
				if (null == url) url = classLoader.getResource("/" + resource);
				if (null != url) return url;
			}
		}
		
		return null;
	}
	
	Class<?> classForName(String name, ClassLoader[] classLoaderArray) throws ClassNotFoundException {
		for (ClassLoader classLoader : classLoaderArray) {
			if (null != classLoader) {
				try {
					Class<?> clazz = Class.forName(name, true, classLoader);
					if (null != clazz) return clazz;
				}
				catch (ClassNotFoundException e) {}
			}
		}
		
		throw new ClassNotFoundException("Cannot find class: " + name);
	}
	
	ClassLoader[] getClassLoaders(ClassLoader classLoader) {
		return new ClassLoader[] {
				classLoader,
				defaultClassLoader,
				Thread.currentThread().getContextClassLoader(),
				getClass().getClassLoader(),
				systemClassLoader
		};
	}
}

  

ResourcesUtils.java
package org.utils.resource;

import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Properties;

public class ResourcesUtils {
	
	private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
	
	private static Charset charset;
	
	public static ClassLoader getDefaultClassLoader() {
		return classLoaderWrapper.defaultClassLoader;
	}
	
	public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
		classLoaderWrapper.defaultClassLoader = defaultClassLoader;
	}
	
	public static Charset getCharset() {
		return charset;
	}
	
	public static void setCharset(Charset charset) {
		ResourcesUtils.charset = charset;
	}
	
	public static URL getResourceURL(String resource) throws IOException {
		return getResourceURL(null, resource);
	}
	
	public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
		URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
		if (url == null) throw new IOException("Could not find resource " + resource);
		
		return url;
	}
	
	public static InputStream getResourceAsStream(String resource) throws IOException {
		return getResourceAsStream(null, resource);
	}
	
	public static InputStream getResourceAsStream(ClassLoader loader, String resource)
			throws IOException {
		InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
		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 = getResourceAsStream(resource);
		props.load(in);
		in.close();
		
		return props;
	}
	
	public static Properties getResourceAsProperties(ClassLoader loader, String resource)
			throws IOException {
		Properties props = new Properties();
		InputStream in = getResourceAsStream(loader, resource);
		props.load(in);
		in.close();
		
		return props;
	}
	
	public static Properties getResourceAsProperties(File file) throws IOException {
		Properties props = new Properties();
		if (!file.exists()) return null;
		InputStream in = new FileInputStream(file);
		props.load(in);
		in.close();
		
		return props;
	}
	
	public static Reader getResourceAsReader(String resource) throws IOException {
		Reader reader;
		if (charset == null) reader = new InputStreamReader(getResourceAsStream(resource));
		else reader = new InputStreamReader(getResourceAsStream(resource), charset);
		
		return reader;
	}
	
	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, URISyntaxException {
		return new File(getResourceURL(resource).toURI());
	}
	
	public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException
			, URISyntaxException {
		return new File(getResourceURL(loader, resource).toURI());
	}
	
	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 {
		Reader reader;
		if (charset == null) reader = new InputStreamReader(getUrlAsStream(urlString));
		else reader = new InputStreamReader(getUrlAsStream(urlString), charset);
		
		return reader;
	}
	
	public static Properties getUrlAsProperties(String urlString) throws IOException {
		Properties props = new Properties();
		InputStream in = getUrlAsStream(urlString);
		props.load(in);
		in.close();
		
		return props;
	}
	
	public static Class<?> classForName(String className) throws ClassNotFoundException {
		return classLoaderWrapper.classForName(className);
	}
}

  

posted @ 2017-11-14 14:44  星瑞  阅读(729)  评论(0编辑  收藏  举报