首頁 > Java > java教程 > 主體

springboot如何讀取resources下的文件

PHPz
發布: 2023-05-12 11:46:15
轉載
2572 人瀏覽過

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

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

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

springboot如何讀取resources下的文件

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下的文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!