springboot如何读取resources中的文件呢?

戚薇 SpringBoot 发布时间:2023-05-16 21:17:37 阅读数:17117 1
下文笔者讲述SpringBoot读取resource中文件的方法分享,如下所示

读取resource中文件的实现思路

方式1:
   使用File读取指定路径
 
方式2:
   使用org.springframework.util.ResourceUtils

方式3:
   使用org.springframework.core.io.ClassPathResource
   读取

方式4:
   使用org.springframework.core.io.ResourceLoader
   读取
例:读取src/main/resources/resource.properties文件

File file = new File("src/main/resources/resource.properties");

@Test
public void testReadFile2() throws IOException {
	File file = new File("src/main/resources/resource.properties");
	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();
}

使用org.springframework.util.ResourceUtils
读取
在linux环境中无法读取

File file = ResourceUtils.getFile("classpath:resource.properties");
FileInputStream fis = new FileInputStream(file);
@Test
public void testReadFile3() throws IOException {
	File file = ResourceUtils.getFile("classpath:resource.properties");
	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();
}

使用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("resource.properties");
	Resource resource = new ClassPathResource("resource.properties");
	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();
}

使用org.springframework.core.io.ResourceLoader;类的注解

package com.java265;
 
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 EttpCustomApplicationTests {
 
    @Autowired
    ResourceLoader resourceLoader;
    
    
    @Test
    public void testReaderFile() throws IOException {
        Resource resource = resourceLoader.getResource("classpath:resource.properties");
        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();
    }
 
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringBoot/202305/6478.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者