java獲取資源文件的幾種方式


第一種采用class方式加載:

public InputStream getResourceAsStream(String pathToConfigFile);

舉例:

舉個例子,在IntelliJ Idea中創建一個java工程,目錄結構如下:

 

該工程里有兩個resources文件夾,一個位於davenkin文件夾下,一個直接位於src文件夾下。第一個resources文件夾下有一個config.properties文件,其內容為:

 

package davenkin;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ResourceLoader
{
    public static void main(String[] args) throws IOException
    {
        ResourceLoader resourceLoader = new ResourceLoader();
        resourceLoader.loadProperties1();

    }

    public void loadProperties1() throws IOException
    {
        InputStream input = null;
        try
        {
            input = Class.forName("davenkin.ResourceLoader").getResourceAsStream("/resources/config.properties");

            //also can be this way:
            //input = this.getClass().getResourceAsStream("/resources/config.properties");
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        printProperties(input);
    }

    private void printProperties(InputStream input) throws IOException
    {
        Properties properties = new Properties();
        properties.load(input);
        System.out.println(properties.getProperty("name"));
    }
}


輸出結果為第二個resources文件夾下config.properties的內容:

ConfigUnderSrc

 

原因在於(請注意ReourceLoader.java文件中的紅色部分):我們給出的資源文件路徑(/resources/config.properties)以"/"開頭,即使用的是絕對定位方式,所以找到的是直接在classpath下的resources文件夾。如果去掉資源文件文件路徑前的"/",則采用的是相對定位方式,此時應該輸出davenkin/resources/config.properties文件的內容。

 

第二種采用classLoader類加載資源文件

public InputStream getResourceAsStream(String pathToConfigFile);

 

用ClassLoader加載配置文件時,pathToConfigFile均不能以"/"開頭,在查找時直接在classpath下進行查找。Class類在查找資源文件時,也是代理(delegate)給ClassLoader完成查找功能的,請參考Java官方文檔

在使用Class和ClassLoader加載資源文件時,有幾種區別細微的方法,修改ResourceLoader.java文件如下:

 

package davenkin;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ResourceLoader
{
    public static void main(String[] args) throws IOException
    {
        ResourceLoader resourceLoader = new ResourceLoader();
        resourceLoader.loadProperties1();
        resourceLoader.loadProperties2();
        resourceLoader.loadProperties3();
        resourceLoader.loadProperties4();
        resourceLoader.loadProperties5();
        resourceLoader.loadProperties6();
    }

    public void loadProperties1() throws IOException
    {
        InputStream input = null;
        try
        {
            input = Class.forName("davenkin.ResourceLoader").getResourceAsStream("/resources/config.properties");
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        printProperties(input);
    }

    public void loadProperties2() throws IOException
    {
        InputStream input = null;

        input = this.getClass().getResourceAsStream("/resources/config.properties");
        printProperties(input);
    }

    public void loadProperties3() throws IOException
    {
        InputStream input = this.getClass().getResourceAsStream("resources/config.properties");
        printProperties(input);
    }

    public void loadProperties4() throws IOException
    {
        InputStream input = this.getClass().getClassLoader().getResourceAsStream("resources/config.properties");
        printProperties(input);
    }

    public void loadProperties5() throws IOException
    {
        InputStream input = ClassLoader.getSystemResourceAsStream("resources/config.properties");
        printProperties(input);
    }

    public void loadProperties6() throws IOException
    {
        InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream("resources/config.properties");

        printProperties(input);
    }

    private void printProperties(InputStream input) throws IOException
    {
        Properties properties = new Properties();
        properties.load(input);
        System.out.println(properties.getProperty("name"));
    }
}

第三種通過spring的Resoruce接口來加載資源也是最方便和最有效的方法:
boolean exists() 資源是否存在
boolean isOpen() 資源是否打開
URL getUEL() throws IOException如果底層資源可以表示成URL,則該方法返回URL對象。
FIle getFlie() 獲取file對象
InputStream getInputStream() 獲取輸入流

Resoruce有許多實現類不同的實現類加載不同的文件
1.WritableResource接口 實現類包括FileSystemResource和PathResource
2.ByteArrayResource 二進制數組表示資源
3.ClassPathResource:類路徑下的資源,資源以相對於類路徑的方式表示。
4.InputStreamResource:以輸入流返回表示的資源
5.ServletContextResourse: 為訪問Web容器的上下文的資源而設計的類以相對web應用根目錄的路徑加載資源
6.UrlResource:訪問http資源和FTP資源。
7.PathResource:可以任意訪問所有的資源。


資源加載器ResourceLoader:
getFile();
getURI()
getEesource();

支持ant風格
ResourcePatternResoler resolver=new PathMatchingResourcePatternResolver();
Resource resources[] =resolver.getResources("classpath*:com/smart/**/*.xml");

 


免責聲明!

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



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