zoukankan      html  css  js  c++  java
  • Java中的资源文件加载方式

    Java中的两种路径

    • 使用文件系统自带的路径机制,一个应用程序只能有一个当前目录,但可以有Path变量来访问多个目录
    • 使用ClassPath路径机制,类路径跟Path全局变量一样也是有多个值

    在Java中,优先使用ClassPath,因为很多程序库在更改路径时更改的是ClassPath而不是Path,比如maven项目把resources目录添加到了classpath里面而没有更改path。在这两种路径表达方式中,/都表示根路径。
    可以说:path方式使用的是文件系统的方式;classpath使用的是java世界的方式。

    API

    ClassLoader 提供了两个方法用于从装载的类路径中取得资源:

    • public URL getResource (String name);
    • public InputStream getResourceAsStream (String name);

    但是真正使用的不是ClassLoader的这两个方法,而是Class的 getResource和getResourceAsStream方法,因为Class对象可以从你的类得到(如YourClass.class或 YourClass.getClass()),而ClassLoader则需要再调用一次YourClass.getClassLoader()方法,不过根据JDK文档的说法,Class对象的这两个方法其实是“委托”(delegate)给装载它的ClassLoader来做的,所以只需要使用 Class对象的这两个方法就可以了。
    因此,直接调用 this.getClass().getResourceAsStream(String name) ;获取流,静态化方法中则使用ClassLoader.getSystemResourceAsStream (String name) ; 。

    下面是一些得到classpath和当前类的绝对路径的一些方法。你可能需要使用其中的一些方法来得到你需要的资源的绝对路径。

    1. this.getClass().getResource("")
      得到的是当前类class文件的URI目录。不包括自己!
      如:file:/D:/workspace/jbpmtest3/bin/com/test/

    2. this.getClass().getResource("/")
      得到的是当前的classpath的绝对URI路径 。
      如:file:/D:/workspace/jbpmtest3/bin/

    3. this.getClass() .getClassLoader().getResource("")
      得到的也是当前ClassPath的绝对URI路径 。
      如:file:/D:/workspace/jbpmtest3/bin/

    4. ClassLoader.getSystemResource("")
      得到的也是当前ClassPath的绝对URI路径 。
      如:file:/D:/workspace/jbpmtest3/bin/

    5. Thread.currentThread().getContextClassLoader ().getResource("")
      得到的也是当前ClassPath的绝对URI路径 。
      如:file:/D:/workspace/jbpmtest3/bin/

    6. ServletActionContext.getServletContext().getRealPath(“/”)
      Web应用程序 中,得到Web应用程序的根目录的绝对路径。这样,我们只需要提供相对于Web应用程序根目录的路径,就可以构建出定位资源的绝对路径。
      如:file:/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebProject

    注意点:

    1. 尽量不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径。这是一颗定时炸 弹,随时可能要你的命。
    2. 尽量使用URI形式的绝对路径资源。它可以很容易的转变为URI,URL,File对象。
    3. 尽量使用相对classpath的相对路径。不要使用绝对路径。使用上面ClassLoaderUtil类的public static URL getExtendResource(String relativePath)方法已经能够使用相对于classpath的相对路径定位所有位置的资源。
    4. 绝对不要使用硬编码的绝对路径。因为,我们完全可以使用ClassLoader类的getResource("")方法得到当前classpath的绝对路径。如果你一定要指定一个绝对路径,那么使用配置文件,也比硬编码要好得多!

    获得CLASSPATH之外路径的方法:
    URL base = this.getClass().getResource(""); //先获得本类的所在位置,如/home/popeye/testjava/build/classes/net/
    String path = new File(base.getFile(), "……/……/……/"+name).getCanonicalPath(); //就可以得到/home/popeye/testjava/name

    在Spring中,有一个ClassPathResource类,是对上述类的封装,其实只需要几行代码就能实现。

    资源文件路径变换

    四种事物:URL、URI、Path、File
    getResource()函数返回的是URL,URL可以转成File和URI,URI可以转成Path,有了Path就可以使用新API中的Files静态方法了。

    URL url = Test.class.getResource("");
    System.out.println("url:" + url + " type:" + url.getClass());
    System.out.println("url.path:" + url.getPath() + " type:" + url.getPath().getClass());
    System.out.println("url.file:" + url.getPath() + " type:" + url.getPath().getClass());
    URI uri = url.toURI();
    System.out.println("uri:" + uri + " type:" + uri.getClass());
    Path p = Path.of(uri);
    System.out.println("path:" + p.toAbsolutePath() + " type:" + p.getClass());
    
  • 相关阅读:
    Visifire正式版(v1.1)发布
    [转]PSP机能强大!已能模拟运行WINDOWS系统?
    在Silverlight+WCF中应用以角色为基础的安全模式(一)基础篇之角色为基础的安全模式简介 Virus
    C#的加密解密算法,包括Silverlight的MD5算法 Virus
    MMORPG programming in Silverlight Tutorial (10)Implement the sprite’s 2D animation (Part IV)
    Game Script: Rescue Bill Gates
    MMORPG programming in Silverlight Tutorial (9)KeyFrame Animation
    MMORPG programming in Silverlight Tutorial (5)Implement the sprite’s 2D animation (Part II)
    MMORPG programming in Silverlight Tutorial (7)Perfect animation
    MMORPG programming in Silverlight Tutorial (3)Animate the object (Part III)
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/6336999.html
Copyright © 2011-2022 走看看