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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM