现在的位置: 首页 > 综合 > 正文

java Object的魅力(加载资源的方式的工具类)

2017年08月13日 ⁄ 综合 ⁄ 共 4424字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.