java Object的魅力(加载资源的方式的工具类)
来源:互联网 发布:php源码学习 编辑:程序博客网 时间:2024/05/16 03:30
Object类可以做什么使用呢?我想大多数人都没有仔细思考过吧,今天带大家领略一下Object的魅力,准确的是或是扩展Object类的魅力。首先要定义两个属性:
private static ClassLoader defaultClassLoader;
private static Charset charset;
在资源处理过程中,类加载器和编码方式是必不可是两个基本属性。在Resources分类中,可以分为:URL,InputStream,Properties,Reader,File
需要注意的是:InputStream和Properties的关系,Properties.load(InputStream);
而在加载类的时候,可以自己指定类的加载器,也可以不指定类的加载器,这就是getClassLoader函数做的事情。指定ClassLoader的目的是为了制定类加载的搜索范围。
下面可以看看:getResourceURL(ClassLoader loader, String resource)函数,搜索的时候就是分为当前类加载器目录下搜索和系统类加载器目录下搜索。
其实,无论是什么类型的资源,其本质和字节码类型都是相同的,都是委托给类加载器去加载的。类加载器加载资源的方式有两种方式:getResource和getResourceAsStream。前者获得Url类,后者获得InputStream类
public class Resources extends Object { 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 { 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 { 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) { } 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 { try { return ClassInfo.getInstance(clazz).instantiateClass(); } catch (Exception e) { 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; }}
转载自:http://www.strutshome.com/index.php/archives/804
0 0
- java Object的魅力(加载资源的方式的工具类)
- java 源码的魅力
- 加载声音资源的工具
- velocity 的资源加载方式
- UE4中资源加载资源的方式
- UE4中资源加载资源的方式
- java加载资源文件的几种方式
- 简述java加载资源文件的三种方式
- java类加载资源的问题
- Spring加载静态资源的方式
- 加载资源文件的三种方式
- ServletContext加载资源文件的方式
- Kylin资源加载的方式 一
- Sciter:加载资源的几种方式
- 反射加载资源的3中方式
- unity中资源动态加载的方式
- 加载文件资源的三种方式
- 黑马程序员——【Java】【高新技术】使用类加载器的方式管理资源和配置文件
- HDU 1599 find the mincost route 、 poj 1734 Sightseeing trip
- OC语法<4.3> Foundation框架中的集合类-NSDictionary
- ARM汇编的一点想法
- cocos2d-x3.2中error C2440: “类型转换”: 无法从“void (__thiscall xx::* )(void)”转换为
- VAADIN应用在Tomcat下部署下的出现ClassNotFoundException问题
- java Object的魅力(加载资源的方式的工具类)
- Hadoop 中SequenceFile的简介
- String,StringBuffer,StringBuilder之间的区别
- 西游记
- NAS的版图
- mahout从零开始
- 通过指针的指针来实现传出参数
- 局域网内不同主机之间实现数据库共享
- MySQL备份恢复之XtraBackup