zoukankan      html  css  js  c++  java
  • classpath获取--getResource()

    在java中的API里,有两种方式来使用classpath读取资源。

    1. Class的getResource()

    2. ClassLoader的getResource()

    但是两者有一定区别,运行以下程序:

    package zero.xml.config;
    
    public class Main {
    
        public static void main(String[] args) {
            new Main().testGetResource();
        }
        
        public void testGetResource() {
            
            System.out.println(Main.class.getResource("/").getPath());
            System.out.println(Main.class.getResource("/app.properties").getPath());
            System.out.println(Main.class.getResource("").getPath());
            System.out.println(Main.class.getResource("app.properties").getPath());
            System.out.println("-------------------");
            System.out.println(this.getClass().getResource("/").getPath());
            System.out.println(this.getClass().getResource("/app.properties").getPath());
            System.out.println(this.getClass().getResource("").getPath());
            System.out.println(this.getClass().getResource("app.properties").getPath());
            System.out.println("-------------------");
            System.out.println(Main.class.getClassLoader().getResource("").getPath());
            System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());
            System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());
            System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());
        }
    }

    得到输出为:

    /home/rain/git/spring-self-learn/bin/
    /home/rain/git/spring-self-learn/bin/app.properties
    /home/rain/git/spring-self-learn/bin/zero/xml/config/
    /home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
    -------------------
    /home/rain/git/spring-self-learn/bin/
    /home/rain/git/spring-self-learn/bin/app.properties
    /home/rain/git/spring-self-learn/bin/zero/xml/config/
    /home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
    -------------------
    /home/rain/git/spring-self-learn/bin/
    /home/rain/git/spring-self-learn/bin/app.properties
    /home/rain/git/spring-self-learn/bin/zero/xml/config
    /home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties

    也就是:

    1. 如果想获得classpath,使用以下方法:

    System.out.println(Main.class.getResource("/").getPath());
    
    System.out.println(Main.class.getClassLoader().getResource("").getPath());

    2. 如果想获得classpath下的文件,使用以下方法:

    System.out.println(Main.class.getResource("/app.properties").getPath());
    
    System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());

    3. 如果想获得当前类(比如zero.xml.config.Main)的路径,使用以下方法:

    System.out.println(Main.class.getResource("").getPath());
    
    System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());

    4. 如果想获得当前类路径下的文件,使用以下方法:

    System.out.println(Main.class.getResource("app.properties").getPath());
    
    System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());

    注意,如果获取的文件或路径不存在,getResource()会返回null。比如,getClassLoader().getResource("/")就会返回null。

  • 相关阅读:
    HearthBuddy投降插件2019-11-01的使用
    正则表达式在线分析 regex online analyzer
    Tips to write better Conditionals in JavaScript
    The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
    Cannot capture jmeter traffic in fiddler
    JMETER + POST + anti-forgery token
    input type color
    HearthBuddy修改系统时间
    What are all the possible values for HTTP “Content-Type” header?
    UDK性能优化
  • 原文地址:https://www.cnblogs.com/drizzlewithwind/p/5721890.html
Copyright © 2011-2022 走看看