首页 技术 正文
技术 2022年11月13日
0 收藏 765 点赞 2,921 浏览 5680 个字
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);
}
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898