`
vyloy
  • 浏览: 78651 次
  • 性别: Icon_minigender_1
  • 来自: 佛山
社区版块
存档分类
最新评论

不依赖系统路径获得资源。getResource()和getResourceAsStream()

阅读更多
ClassLoader.getSystemResource();
ClassLoader.getSystemResourceAsStream();
//这些方法用来获得在同一CLASSPATH中的资源


System Resources

A system resource is a resource that is either built-in to the system, or kept by the host implementation in, for example, a local file system. Programs access system resources through the ClassLoader methods getSystemResource and getSystemResourceAsStream.

For example, in a particular implementation, locating a system resource may involve searching the entries in the CLASSPATH. The ClassLoader methods search each directory, ZIP file, or JAR file entry in the CLASSPATH for the resource file, and, if found, returns either an InputStream, or the resource name. If not found, the methods return null. A resource may be found in a different entry in the CLASSPATH than the location where the class file was loaded.

System resources are those that are handled by the host implemenation directly. For example, they may be located in the CLASSPATH.(当要定位的资源是本地直接实现的,例如:是位于在CLASSPATH中的)

The methods in ClassLoader use the given String as the name of the resource without applying any absolute/relative transformation (see the methods in Class). The name should not have a leading "/".(这些在ClassLoader内的方法不会进行绝对、相对地址转换,所以所传的参数name不应该以“/”开头)

ClassLoader.getResource()
ClassLoader.getResourceAsStream()
//这些方法可以根据不同的ClassLoader有不同获得方法。
//例如:AppletClassLoader,可以通过网络获得资源。


Non-System Resources

The implementation of getResource on a class loader depends on the details of the ClassLoader class. For example, AppletClassLoader:

  • First tries to locate the resource as a system resource; then, if not found,
  • Searches through the resources in archives (JAR files) already loaded in this CODEBASE; then, if not found,
  • Uses CODEBASE and attempts to locate the resource (which may involve contacting a remote site).

All class loaders will search for a resource first as a system resource, in a manner analogous to searcing for class files. This search rule permits overwriting locally any resource. Clients should choose a resource name that will be unique (using the company or package name as a prefix, for instance).

Class.getResource()
Class.getResourceAsStream()
//在不确定类的加载方式时使用。
//会根据加载类的ClassLoader去决定怎样的方式去加载资源。
//就像ClassLoader加载Class一样。例如是AppletClassLoader网络加载,提供很好的灵活性。


会先把参数作绝对地址和相对地址转换。再委派给ClassLoader相对应的方法。
“/”开头的地址就是绝对地址,否则就是相对class的地址(会在地址前加上class的路径)。实现转换的方法如下:

private String resolveName(String name) {
  if (name == null) {
    return name;
  }
  if (!name.startsWith("/")) {
    Class c = this;
    while (c.isArray()) {
      c = c.getComponentType();
    }
    String baseName = c.getName();
    int index = baseName.lastIndexOf('.');
    if (index != -1) {
      name = baseName.substring(0, index).replace('.', '/') + "/" + name;
    }
  } else {
    name = name.substring(1);
  }
  return name;
}


参考:http://docs.oracle.com/javase/1.5.0/docs/guide/lang/resources.html
分享到:
评论

相关推荐

    java-loadresource.rar

    Java加载资源文件的两种方法getResource与getResourceAsStream

    getResourceAsStream

    java getResourceAsStream种类,分我所知道的3种情况

    Java 使用getClass().getResourceAsStream()方法获取资源

    主要介绍了Java 使用getClass().getResourceAsStream()方法获取资源的相关资料,这里主要讲解哪种方式可以获取到文件资源,需要的朋友可以参考下

    Java中getResourceAsStream用法分析

    主要介绍了Java中getResourceAsStream用法,较为详细的分析了getResourceAsStream的功能及用法,需要的朋友可以参考下

    Java中getResourceAsStream的用法.md

    Java中getResourceAsStream的用法.md

    加载资源文件类

    资源文件加载类 这里是用的是class getResourceAsStream "path" 来加载资源文件的

    javaEE-web的文件路径

    javaEE-web的文件路径, 用getClass().getResourceAsStream("/")来确定文件路径 并用jdom读取xml和下载1

    android-studio-unit-test-resource-issue-demo:演示执行单元测试时缺少Java资源的问题

    getResourceAsStream( " path/to/resource.txt " ); 此问题影响非Android单元测试和Java资源,仅在具有“单元测试”配置的Android Studio上执行。 它不影响使用Gradle(在Android Studio或命令行中)执行测试用例...

    MyBatis3.2.3帮助文档(中文版).zip

    XML 配置文件(configuration XML)中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。XML 配置文件的详细内容后面...

    day020-继承加强和设计模式代码和笔记.rar

    2、InputStream is = classLoader.getResourceAsStream("文件路径/文件名");//通过当前线程的类加载器获取流对象,如果是源文件夹,直接文件名 建议用当前线程类的加载器方式获取流,线程安全问题 ...

    classes.sql

    lass.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取。其实也是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。

    jsp内置对象的用法

    11 URL getResource(String path) 返回指定资源(文件及目录)的URL路径 12 InputStream getResourceAsStream(String path) 返回指定资源的输入流 13 RequestDispatcher getRequestDispatcher(String uripath) ...

    图书馆管理系统

    InputStream in=getClass().getResourceAsStream(propFileName); prop.load(in); //通过输入流对象加载Properties文件 dbClassName = prop.getProperty("DB_CLASS_NAME"); //获取数据库驱动 dbUrl =...

    使用J2SE API读取Properties文件的六种方法

    ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());  3。使用java.util.PropertyResourceBundle类的构造函数  示例: InputStream in = new BufferedInputStream(new FileInputStream...

    dom4j.jar.jar

    InputStream is = Demo1.class.getResourceAsStream("/students.xml");// src目录下 // 1. 获得文档解析器工厂对象 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // 2. ...

    DruidJDBCUtils.java

    InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e....

    Aspose.Words.jdk16_c处理word文档,不带水印版本

    is = new Document().getClass().getResourceAsStream("/resources/aspose.word.license.xml"); if(is==null) throw new RuntimeException("Cannot find licenses file. Please contact wdmsyf@yahoo....

    aspose-11.0.0.jar

    InputStream license = ChangePDF.class.getClassLoader().getResourceAsStream("\\license.xml"); try { new License().setLicense(license); } catch (Exception e) { e.printStackTrace(); } Document ...

    java加载属性配置文件(properties文件)——从入门到进阶

    java加载属性配置文件[properties文件]什么是properties文件为什么要使用properties文件使用java加载properties文件的两种方式使用类的加载器获得输入流加载文件getResourceAsStream()介绍使用文件输入流加载文件...

    java工厂系列设计模式源码与文档

    Document document = sb.build(this.getClass().getClassLoader().getResourceAsStream(fileName)); Element root = document.getRootElement(); List list = XPath.selectNodes(root, "/beans/bean"); for...

Global site tag (gtag.js) - Google Analytics