springboot讀取resources下文件的方式詳解

項目中很多時候需要讀取自定義配置文件,本地開發工具怎麼寫都成功但是部署到服務其上就出現問題,

異常BOOT-INF/classes!/config.xml (文件名、目錄名或卷標語法不正確.)路徑中帶有嘆號之類的

瞭解瞭大概之後就是springboot打成jar是一個文件,也就是一個壓縮包,沒有辦法讀取壓縮文件裡的路徑,因此要解決這個問題瞭解讀取配置文件的原理,直接獲取文件流就可以瞭。

1、使用項目內路徑讀取,隻能在開發工具中使用,部署之後無法讀取。(不通用

類似:src/main/resources/default.xml

File file = new File("src/main/resources/default.xml");

    @Test
    public void testReadFile2() throws IOException {
        File file = new File("src/main/resources/default.xml");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        fis.close();
    }

 2、使用org.springframework.util.ResourceUtils,讀取。在linux環境中無法讀取。(不通用)

File file = ResourceUtils.getFile("classpath:default.xml");
FileInputStream fis = new FileInputStream(file);

    @Test
    public void testReadFile3() throws IOException {
        File file = ResourceUtils.getFile("classpath:default.xml");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        fis.close();
    }

3、使用org.springframework.core.io.ClassPathResource,各種環境都能讀取。(通用)

Resource resource = new ClassPathResource("resource.properties");
InputStream is = resource.getInputStream();

    @Test
    public void testReadFile() throws IOException {
//        ClassPathResource classPathResource = new ClassPathResource("default.xml");
        Resource resource = new ClassPathResource("default.xml");
        InputStream is = resource.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        is.close();
    }

4、結合spring註解,使用org.springframework.core.io.ResourceLoader;類的註解。(通用)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.junit4.SpringRunner;
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
 
    @Autowired
    ResourceLoader resourceLoader;
    
    
    @Test
    public void testReaderFile() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:default.xml");
        InputStream is = resource.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        while((data = br.readLine()) != null) {
            System.out.println(data);
        }
        
        br.close();
        isr.close();
        is.close();
    }
}

總結

到此這篇關於springboot讀取resources下文件的文章就介紹到這瞭,更多相關springboot讀取resources文件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: